快读(快速读入) +借鉴出处:@
- getchar()比scanf要快,故为了加快读入,可以用getchar()代替scanf
- 利用getchar()将数字读入,若为“ ”或”\n”结束,第一个字符判断一下数字的正负,然后每读入一个数字就将当前数*10并加上它
-
inline int read() { char ch = getchar(); int x = 0, f = 1; while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); } while('0' <= ch && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }
-
void read(T& x) { int f = 1; x = 0; char ch = getchar(); while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();} while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();} x *= f; }
如何在c++中提升cin和cout的速度
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}