关于使用fread代替getchar实现快速读入的疑问
  • 板块灌水区
  • 楼主__O_w_O__
  • 当前回复8
  • 已保存回复8
  • 发布时间2024/8/5 11:59
  • 上次更新2024/8/5 13:30:05
查看原帖
关于使用fread代替getchar实现快速读入的疑问
1053567
__O_w_O__楼主2024/8/5 11:59

rt,对于以下代码

#include <bits/stdc++.h>
using namespace std;

const int SIZE = 1 << 14;
inline char gc() {
    static char buf[SIZE], *begin = buf, *end = buf;
    if (begin == end) {
        begin = buf;
        end = buf + fread(buf, 1, SIZE, stdin);
    }
    return *begin++;
}
inline int read() {
    int x = 0, f = 0, c = gc();
    while(c < '0' || c > '9') f |= c == '-', c = gc();
    while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - 48, c = gc();
    return f ? -x : x;
}
int main() {
    int n = read();
    cout << n << endl;
    for (int i = 1; i <= n; i++) {
        int x = read();
        cout << x << endl;
    }
    return 0;
}

在洛谷在线IDE,当输入为

6
5
1
2
5
4
6

(注意输入结尾没有多余的换行)时,输出为

6
5
1
2
5
4
66

然而当输入为

6
5
1
2
5
4
6

(注意输入的结尾有多余的换行)时,输出为

6
5
1
2
5
4
6

不太明白这是什么原因(快读的模板是抄的深进),这种写法在本地(windows)下没有问题,并且当gc换成getchar()或者getchar_unlocked()的时候在洛谷IDE也正常

个人认为应该是读入结尾的换行的问题,有没有dalao给解答一下qaq(

2024/8/5 11:59
加载中...