讀取多行輸入,每行兩個整數 A B,輸出它們的和。直到 EOF 結束。最基礎的「掛牌題」。
Input: 1 1 Output: 2
#include <stdio.h>
int main(void) {
int A, B;
while (scanf("%d %d", &A, &B) == 2) { // 或 != EOF
printf("%d\n", A + B);
}
return 0;
}
while (scanf("%d %d", &A, &B) != EOF) 或 == 2 (成功讀取兩個)。~scanf(...) 同理。printf("%d\n", ...) 確保每行一個結果。int 足夠。int main(void) 返回 0。while (!feof(stdin)) 後跟 scanf:會多執行一次 (EOF 後 feof 才置位)& 取地址\n 導致格式錯誤cin/cout 未關閉同步 (ios::sync_with_stdio(false)) 可能慢 (本題數據極小無影響)#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long A, B; // 防萬一
while (cin >> A >> B) cout << A + B << '\n';
return 0;
}
HDU 1000 看似極簡,實則考察「標準輸入循環讀取直到 EOF」這一競技編程最基礎模式。吃透此題,後續所有「多測試用例、未知行數」輸入處理皆可舉一反三。新手必刷,老手秒過。