這個學期開始接觸 ACM 压昼,查閱相關資料得知宙项,大致有以下八種輸入輸出格式鲸阻,題目來自 HDU 切诀。
A+B for Input-Output Practice (I)
輸入樣例:
1 5
10 20
輸出樣例:
6
30
由上可知诬留,
有若干組輸入數(shù)據(jù)斜纪;有若干組輸出數(shù)據(jù),每組輸出數(shù)據(jù)占一行文兑。
于是盒刚,我們用以下語句來讀取評測系統(tǒng)中輸入文件的內(nèi)容:
while(scanf(...) != EOF)
完整代碼:
#include <stdio.h>
int main(){
int a, b;
while(scanf("%d %d", &a, &b) != EOF){
printf("%d\n", a + b);
}
return 0;
}
A+B for Input-Output Practice (II)
輸入樣例:
2
1 5
10 20
輸出樣例:
6
30
由上可知,
先輸入一個整數(shù)彩届,確定接下來要輸入幾組數(shù)據(jù)伪冰,輸出格式不變。
于是樟蠕,我們可以先用 scanf
確定要輸入多少組數(shù)據(jù)贮聂,然后用for循環(huán)進行輸入
完整代碼:
#include <stdio.h>
int main(){
int a, b, i, N;
scanf("%d", &N);
for(i=0;i<N;i++){
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
}
return 0;
}
A+B for Input-Output Practice (III)
輸入樣例:
1 5
10 20
0 0
輸出樣例:
6
30
由上可知,
有若干組輸出數(shù)據(jù)寨辩,題目給定了輸入結束條件即輸入 0 0
吓懈,輸出不變。
于是靡狞,我們根據(jù)上一題耻警,在 while
循環(huán)條件加上判斷條件或在內(nèi)部加上判斷條件即可
完整代碼:
#include <stdio.h>
int main(){
int a, b;
while (scanf("%d %d", &a ,&b) && (a != 0 || b != 0)){
printf("%d\n", a + b);
}
return 0;
}
A+B for Input-Output Practice (IV)
輸入樣例:
4 1 2 3 4
5 1 2 3 4 5
0
輸出樣例:
10
15
由上可知,
輸入數(shù)據(jù)有若干組甸怕,每組第一個數(shù)字確定接下來該組的數(shù)據(jù)個數(shù)甘穿,為0時結束。
于是梢杭,我們加上判斷條件循環(huán)輸入輸出
完整代碼:
#include <stdio.h>
int main(){
int a, N, i, sum;
while (scanf("%d", &N) && N != 0){
sum = 0;
for(i=0;i<N;i++){
scanf("%d", &a);
sum += a;
}
printf("%d\n", sum);
}
return 0;
}
A+B for Input-Output Practice (V)
輸入樣例:
2
4 1 2 3 4
5 1 2 3 4 5
輸出樣例:
10
15
由上可知温兼,
先輸入一個整數(shù),確定接下來要輸入幾組數(shù)據(jù)武契;每組輸入數(shù)據(jù)的第一個數(shù)據(jù)募判,確定該組的數(shù)據(jù)個數(shù)。
于是咒唆,我們用兩個循環(huán)實現(xiàn)
完整代碼:
#include <stdio.h>
int main(){
int a, N, i, j, sum, n;
scanf("%d", &n);
for(i=0;i<n;i++){
scanf("%d", &N);
sum = 0;
for(j=0;j<N;j++){
scanf("%d", &a);
sum += a;
}
printf("%d\n", sum);
}
return 0;
}
A+B for Input-Output Practice (VI)
輸入樣例:
4 1 2 3 4
5 1 2 3 4 5
輸出樣例:
10
15
由上可知届垫,
有若干組輸入數(shù)據(jù),當讀取完畢時結束全释,輸出所有數(shù)據(jù)装处,與上題類似
完整代碼:
#include <stdio.h>
int main(){
int a, N, j, sum;
while (scanf("%d", &N) != EOF){
sum = 0;
for(j=0;j<N;j++){
scanf("%d", &a);
sum += a;
}
printf("%d\n", sum);
}
return 0;
}
A+B for Input-Output Practice (VII)
輸入樣例:
1 5
10 20
輸出樣例:
6
30
由上可知,
與第一題類似浸船,只是多輸出了一個空行
完整代碼:
#include <stdio.h>
int main(){
int a, b;
while (scanf("%d %d", &a, &b) != EOF){
printf("%d\n\n", a + b);
}
return 0;
}
A+B for Input-Output Practice (VIII)
輸入樣例:
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
輸出樣例:
10
15
6
本題輸出格式可能會疏忽符衔,即最后一行輸出后沒有空行
完整代碼:
#include <stdio.h>
int main(){
int a, i, sum, j, N, n;
scanf("%d", &N);
for(i=0;i<N;++i){
sum = 0;
scanf("%d", &n);
for(j=0;j<n;++j){
scanf("%d", &a);
sum += a;
}
printf("%d\n", sum);
if(N != i+1){
printf("\n");
}
}
return 0;
}