read函数(scanf效率的2倍):
int read() {
int ret = 0, sgn = 0, ch = getchar();
while (!isdigit(ch)) sgn |= ch == '-', ch = getchar();
while (isdigit(ch)) ret = ret * 10 + ch - '0', ch = getchar();
return sgn ? -ret : ret;
}
read函数优化(scanf效率的3倍):
const int SIZE = 1 << 14;
char getc() {
static char buf[SIZE], *begin = buf, *end = buf;
if (begin == end) begin = buf, end = buf + fread(buf, 1, SIZE, stdin);
return *begin ++ ;
}
int read() {
int ret = 0, sgn = 0, ch = getc();
while (!isdigit(ch)) sgn |= ch == '-', ch = getc();
while (isdigit(ch)) ret = ret * 10 + ch - '0', ch = getc();
return sgn ? -ret : ret;
}
注:scanf
的输入效率是未开流同步cin
的4倍