读入读出挂!!
说起读入读出挂,体内洪荒之力呼啸飞过,输入输出规模超过 10e6 就可以用啦;
参考博客:https://blog.csdn.net/f_zyj/article/details/51473493
适用于正整数
void read(int &x){ char ch = getchar();x = 0; for (; ch < '0' || ch > '9'; ch = getchar()); for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; }
1 template <class T> 2 inline void scan_d(T &ret) 3 { 4 char c; 5 ret = 0; 6 while ((c = getchar()) < '0' || c > '9'); 7 while (c >= '0' && c <= '9') 8 { 9 ret = ret * 10 + (c - '0'), c = getchar(); 10 } 11 }
适用于正负整数
1 template <class T> 2 inline bool scan_d(T &ret) 3 { 4 char c; 5 int sgn; 6 if (c = getchar(), c == EOF) 7 { 8 return 0; //EOF 9 } 10 while (c != '-' && (c < '0' || c > '9')) 11 { 12 c = getchar(); 13 } 14 sgn = (c == '-') ? -1 : 1; 15 ret = (c == '-') ? 0 : (c - '0'); 16 while (c = getchar(), c >= '0' && c <= '9') 17 { 18 ret = ret * 10 + (c - '0'); 19 } 20 ret *= sgn; 21 return 1; 22 } 23 24 template <class T> 25 inline void print_d(T x) 26 { 27 if (x > 9) 28 { 29 print_d(x / 10); 30 } 31 putchar(x % 10 + '0'); 32 }
仅适合纯数字输入输出
1 int Scan() 2 { // 输入外挂 3 int res = 0, flag = 0; 4 char ch; 5 if ((ch = getchar()) == '-') 6 { 7 flag = 1; 8 } 9 else if(ch >= '0' && ch <= '9') 10 { 11 res = ch - '0'; 12 } 13 while ((ch = getchar()) >= '0' && ch <= '9') 14 { 15 res = res * 10 + (ch - '0'); 16 } 17 return flag ? -res : res; 18 } 19 20 void Out(int a) 21 { // 输出外挂 22 if (a < 0) 23 { 24 putchar('-'); 25 a = -a; 26 } 27 if (a >= 10) 28 { 29 Out(a / 10); 30 } 31 putchar(a % 10 + '0'); 32 } 33 34 int main() 35 { 36 int T, n; 37 scanf ("%d", &T); 38 while (T--) 39 { 40 n = Scan(); 41 Out(n); 42 printf("\n"); 43 } 44 return 0; 45 }
适用于正负数(int,long long,float,double)
1 template <class T> 2 bool scan_d(T &ret) 3 { 4 char c; 5 int sgn; 6 T bit = 0.1; 7 if (c=getchar(), c==EOF) 8 { 9 return 0; 10 } 11 while (c! = '-' && c != '.' && (c < '0' || c > '9')) 12 { 13 c = getchar(); 14 } 15 sgn = (c == '-') ? -1 : 1; 16 ret = (c == '-') ? 0 : (c - '0'); 17 while (c = getchar(), c >= '0' && c <= '9') 18 { 19 ret = ret * 10 + (c - '0'); 20 } 21 if (c == ' ' || c == '\n') 22 { 23 ret *= sgn; 24 return 1; 25 } 26 while (c = getchar(), c >= '0' && c <= '9') 27 { 28 ret += (c - '0') * bit, bit /= 10; 29 } 30 ret *= sgn; 31 return 1; 32 } 33 34 template <class T> 35 inline void print_d(int x) 36 { 37 if (x > 9) 38 { 39 print_d(x / 10); 40 } 41 putchar(x % 10 + '0'); 42 }
套装
1 char buf[MAXIN], *ps = buf, *pe = buf + 1; 2 3 inline void rnext() 4 { 5 if (++ps == pe) 6 { 7 pe = (ps = buf) + fread(buf, sizeof(char), sizeof(buf) / sizeof(char), stdin); 8 } 9 return ; 10 } 11 12 template <class T> 13 inline bool in(T &ans) 14 { 15 ans = 0; 16 T f = 1; 17 if (ps == pe) 18 { 19 return false; 20 } 21 do 22 { 23 rnext(); 24 if ('-' == *ps) 25 { 26 f = -1; 27 } 28 } while (!isdigit(*ps) && ps != pe); 29 if (ps == pe) 30 { 31 return false; 32 } 33 do 34 { 35 ans = (ans << 1) + (ans << 3) + *ps - 48; 36 rnext(); 37 } while (isdigit(*ps) && ps != pe); 38 ans *= f; 39 return true; 40 } 41 42 char bufout[MAXOUT], outtmp[50], *pout = bufout, *pend = bufout + MAXOUT; 43 44 inline void write() 45 { 46 fwrite(bufout, sizeof(char), pout - bufout, stdout); 47 pout = bufout; 48 return ; 49 } 50 51 inline void out_char(char c) 52 { 53 *(pout++) = c; 54 if (pout == pend) 55 { 56 write(); 57 } 58 return ; 59 } 60 61 inline void out_str(char *s) 62 { 63 while (*s) 64 { 65 *(pout++) = *(s++); 66 if (pout == pend) 67 { 68 write(); 69 } 70 } 71 return ; 72 } 73 74 template <class T> 75 inline void out_int(T x) 76 { 77 if (!x) 78 { 79 out_char('0'); 80 return ; 81 } 82 if (x < 0) 83 { 84 x = -x, out_char('-'); 85 } 86 int len = 0; 87 while (x) 88 { 89 outtmp[len++] = x % 10 + 48; 90 x /= 10; 91 } 92 outtmp[len] = 0; 93 for (int i = 0, j = len - 1; i < j; i++, j--) 94 { 95 swap(outtmp[i], outtmp[j]); 96 } 97 out_str(outtmp); 98 return ; 99 }
更加高效的输入输出外挂
1 struct FastIO 2 { 3 static const int S = 100 << 1; 4 5 int wpos; 6 char wbuf[S]; 7 8 FastIO() : wpos(0) {} 9 10 inline int xchar() 11 { 12 static char buf[S]; 13 static int len = 0, pos = 0; 14 15 if (pos == len) 16 { 17 pos = 0; 18 len = (int)fread(buf, 1, S, stdin); 19 } 20 if (pos == len) 21 { 22 return -1; 23 } 24 25 return buf[pos++]; 26 } 27 28 inline int xint() 29 { 30 int s = 1, c = xchar(), x = 0; 31 while (c <= 32) 32 { 33 c = xchar(); 34 } 35 if (c == '-') 36 { 37 s = -1; 38 c = xchar(); 39 } 40 for (; '0' <= c && c <= '9'; c = xchar()) 41 { 42 x = x * 10 + c - '0'; 43 } 44 45 return x * s; 46 } 47 48 ~FastIO() 49 { 50 if (wpos) 51 { 52 fwrite(wbuf, 1, wpos, stdout); 53 wpos = 0; 54 } 55 } 56 } io;