1. 程式人生 > >fread讀入掛and普通讀入掛and浮點數讀入掛

fread讀入掛and普通讀入掛and浮點數讀入掛

val sta digi lin scanf ... www. win 數據塊

fread讀入掛



版本一

namespace fastIO {
  #define BUF_SIZE 100000
  //fread -> read
  bool IOerror = 0;
  inline char nc() {
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if(p1 == pend) {
      p1 = buf;
      pend = buf + fread(buf, 1, BUF_SIZE, stdin);
      if(pend == p1){
        IOerror = 1;
        return -1;
      }
    }
    return *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template<typename T>
  inline void read(T &x) {
    char ch;
    while(blank(ch = nc()));
    if(IOerror)
      return;
    for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
  }
  #undef BUF_SIZE
};

using namespace fastIO;



版本二

struct FastIO {
    static const int S = 1e7;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar() {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) exit(0);
        return buf[pos++];
    }
    inline int xuint() {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint() {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s) {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x) {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos++] = x;
    }
    inline void wint(LL x) {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
        wchar('\n');
    }
    inline void wstring(const char *s) {
        while (*s) wchar(*s++);
    }
    ~FastIO() {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;



版本三

inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int _read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}


版本四

#include<cstdio>
#include<iostream>
using namespace std;
const int maxx=40000008;
char Input[maxx+5],*ipos;
#define read() (strtol(ipos,&ipos,10))

int main() {
    fread(Input,maxx,1,stdin);ipos=Input;
    int n=read();
    
    //Do something......
    return 0;
}



版本五

const int STRSIZE=int(4e7);
char in1[STRSIZE];
char *in=in1, *tempend;
void Input() {
    tempend=in+STRSIZE;
    fread(in,1,STRSIZE,stdin);
}
inline void scan(int &x) {
    char c=*(in++);
    while(!(c>='0' && c<='9')) c=*(in++);
    x=c-'0';
    c=*(in++);
    while(c>='0' && c<='9') {
        x=x*10+c-'0';
        c=*(in++);
    }
}



版本六

const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
    if(head==tail) {
        int l=fread(buffer,1,BufferSize,stdin);
        tail=(head=buffer)+l;
    }
    return *head++;
}
inline int read() {
    int x=0,f=1;char c=Getchar();
    for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
    return x*f;
}
inline void write(int x){
    if(x>=10)write(x/10);
    putchar(x%10+'0');
}



版本七

namespace IO {
    const int MX = 4e7; //1e7占用內存11000kb
    char buf[MX]; int c, sz;
    void begin() {
        c = 0;
        sz = fread(buf, 1, MX, stdin);
    }
    inline bool read(int &t) {
        while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
        if(c >= sz) return false;
        bool flag = 0; if(buf[c] == '-') flag = 1, c++;
        for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
        if(flag) t = -t;
        return true;
    }
}



版本八

struct FastIO {
    static const int S = 100 << 1;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar() {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len) {
            pos = 0;
            len = (int)fread(buf, 1, S, stdin);
        }
        if (pos == len) {
            return -1;
        }
        return buf[pos++];
    }
    inline int xint() {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) {
            c = xchar();
        }
        if (c == '-') {
            s = -1;
            c = xchar();
        }
        for (; '0' <= c && c <= '9'; c = xchar()) {
            x = x * 10 + c - '0';
        }
        return x * s;
    }
    ~FastIO() {
        if (wpos) {
            fwrite(wbuf, 1, wpos, stdout);
            wpos = 0;
        }
    }
} io;



版本九:

namespace fastio{
  int ptr, ye, Siz;
  char temp[25], str[8333667], out[8333669];
  void io_init(){
    ptr = 0, ye = 0;
    Siz = fread(str, 1, 8333667, stdin);
  }
  inline int read(){
    int i, j, val = 0, fla = 1;
    while (ptr<Siz&&(str[ptr] < 48 || str[ptr] > 57)) ptr++;
    if(ptr>=Siz)return -1;
    while (str[ptr] > 47 && str[ptr] < 58) fla = 0,val = (val * 10) + (str[ptr++] - 48);
    if(fla)return -1;
    return val;
  }
  inline void convert(long long x,bool flag){
    int i, d = 0;
    for (; ;){
      temp[++d] = (x % 10) + 48;
      x /= 10;
      if (!x) break;
    }
    for (i = d; i; i--) out[ye++] = temp[i];
    out[ye++] = ' ';
    if(flag) out[ye-1] = 10;
  }
  inline void io_print(){
    fwrite(out, 1, ye, stdout);
  }
}
using namespace fastio;


fread()函數和fwrite()函數:(可以用來實現對數據塊的操作)

// 讀取文件塊數據
size_t fread(void *buffer, size_t size, size_t count, FILE *file);

// 寫入文件塊數據
size_t fwrite(const void *buffer, size_t size, size_t count, FILE *file);

fread參數說明:buffer是讀取數據後存放地址,size是的塊長度,count是塊的數量,實際讀取長度為sizecount,返回值為塊成功讀取塊的count數量。
fwrite參數說明:buffer是寫入數據後存放地址,size是的塊長度,count是塊的數量,實際讀取長度為size
count,返回值為塊成功寫入快的count數量。


普通讀入掛

版本一:

int read(){
    int sum=0;char ch=getchar();
    while(!(ch>='0'&&ch<='9'))ch=getchar();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=getchar();
    return sum;
}


strtok和sscanf結合輸入

/*
 *  空格作為分隔輸入,讀取一行的整數
 */
gets(buf);

int v;
char *p = strtok(but, " "); 
while (p){
    sscanf(p, "%d", &v);
    p = strtok(NULL," "); 
}


浮點數

inline bool scan_lf(double &num)  {
        char in;double Dec=0.1;
        bool IsN=false,IsD=false;
        in=getchar();
        if(in==EOF) return false;
        while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
                in=getchar();
        if(in=='-'){IsN=true;num=0;}
        else if(in=='.'){IsD=true;num=0;}
        else num=in-'0';
        if(!IsD){
                while(in=getchar(),in>='0'&&in<='9'){
                        num*=10;num+=in-'0';}
        }
        if(in!='.'){
                if(IsN) num=-num;
                return true;
        }else{
                while(in=getchar(),in>='0'&&in<='9'){
                        num+=Dec*(in-'0');Dec*=0.1;
                }
        }
        if(IsN) num=-num;
        return true;
}



qsc的讀入掛:傳送門

fread讀入掛and普通讀入掛and浮點數讀入掛