版本比较
卡常导论
正文
增加 34 行删除 34 行
75 行未改动76777879808180818283848586878889909192939490919293949596979899100101989910010110210310410510610710810911011111211311411011111211311411511611711811912012111811912012112212312412516 行未改动14214314414514614714814614714814915015115215215315415515615615715815916016116216316416016116216316416516616716816917017116816917017117217317417514 行未改动75 行未改动
关闭同步流代码:
```cppios::sync_with_stdio(0); cin.tie(0); ios::sync_with_stdio(0);cin.tie(0);```
这时我们可以用到更加快速的 `getchar()` 和 `putchar()` 两个函数。
在读取一个整数时,可以使用以下模版:
```cppinline int read () { char ch; while (ch = getchar(), ch < '0' || ch > '9'); int num = ch - '0'; for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0'); return num; char ch; while (ch = getchar(), ch < '0' || ch > '9'); int num = ch - '0'; for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0'); return num;}
inline void print (int x) { if (x < 0) putchar('-'), x = -x; int res[20], tot = 0; for (; x; res[tot++] = x % 10, x /= 10); for (int i = tot - 1; ~i; putchar(res[i--] + '0')); if (x < 0) putchar('-'), x = -x; int res[20], tot = 0; for (; x; res[tot++] = x % 10, x /= 10); for (int i = tot - 1; ~i; putchar(res[i--] + '0'));}```
可以把 int 换成各种整型,或者改成模版函数:
```cpptemplate<typename T = int>inline T read () { char ch; while (ch = getchar(), ch < '0' || ch > '9'); T num = ch - '0'; for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0'); return num; char ch; while (ch = getchar(), ch < '0' || ch > '9'); T num = ch - '0'; for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0'); return num;}
inline void print (T x) { if (x < 0) putchar('-'), x = -x; int res[20], tot = 0; for (; x; res[tot++] = x % 10, x /= 10); for (int i = tot - 1; ~i; putchar(res[i--] + '0')); if (x < 0) putchar('-'), x = -x; int res[20], tot = 0; for (; x; res[tot++] = x % 10, x /= 10); for (int i = tot - 1; ~i; putchar(res[i--] + '0'));}```
还有更快的方法吗?有的兄弟,有的。16 行未改动#define QREADPRINT
#ifdef QREADPRINT #define BUF_SIZE 114514 char readbuf[BUF_SIZE], printbuf[BUF_SIZE], *readp0, *readp1, *printp0 = printbuf;
char readbuf[BUF_SIZE], printbuf[BUF_SIZE], *readp0, *readp1, *printp0 = printbuf;
#define getchar() (readp0 == readp1 && (readp1 = (readp0 = readbuf) + fread(readbuf, 1, BUF_SIZE, stdin), readp0 == readp1) ? EOF : *readp0++) #define putchar(x) ((printp0 == printbuf + BUF_SIZE) && (fwrite(printbuf, printp0 - printbuf, 1, stdout), printp0 = printbuf), *printp0++ = (x)) #define endputchar() fwrite(printbuf, printp0 - printbuf, 1, stdout)
class QReadPrint { public: ~QReadPrint () { endputchar(); } } qreadprint; } qreadprint;#endif
inline int read () { char ch; while (ch = getchar(), ch < '0' || ch > '9'); int num = ch - '0'; for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0'); return num; char ch; while (ch = getchar(), ch < '0' || ch > '9'); int num = ch - '0'; for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0'); return num;}
inline void print (int x) { if (x < 0) putchar('-'), x = -x; int res[20], tot = 0; for (; x; res[tot++] = x % 10, x /= 10); for (int i = tot - 1; ~i; putchar(res[i--] + '0')); if (x < 0) putchar('-'), x = -x; int res[20], tot = 0; for (; x; res[tot++] = x % 10, x /= 10); for (int i = tot - 1; ~i; putchar(res[i--] + '0'));}```
通过是否定义 QREADPRINT 可以控制是否打开高级快读快写,如果不开就会使用普通的 `getchar()` 和 `putchar()`。14 行未改动