A+B problems
这几道习题大概是ACM输入输出格式的普及
P1:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input:
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
Sample Output
6
30
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
int main()
{
int a,b;
while("scanf("%d %d",&a,&b)==2")
printf("%d\n",a+b);
return 0;
} //简单的用while来控制多组数据的输入
P2:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
2
1 5
10 20
Sample Output
6
30
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
int main()
{
int a,b,k;
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d %d",&a,&b);
printf("%d\n",a+b);
}
return 0;
} //该题关键在于利用循环做一个组数的遍历
P3:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing
0 0 terminates the input and this test case is not to be processed.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
0 0
Sample Output
6
30
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)==2&&(a!=0)||(b!=0))
printf("%d\n",a+b);
return 0;
} //利用逻辑判断符使得输入0 0时跳出循环;这点我想在循环体内利用if+break语句也能实现
P4:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with
0 terminates the input and this test case is not to be processed.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample Input
4 1 2 3 4
5 1 2 3 4 5
0
Sample Output
10
15
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include<stdio.h>
int main()
{
int a,b;
int sum;
while(scanf("%d",&a)!=EOF&&a!=0)
{ sum=0;
for(int i=1;i<=a;i++)
{
scanf("%d",&b);
sum+=b;
}
printf("%d\n",sum);
}
return 0;
} //这题比较有意思,关键在于利用while做一个先导的组数限定,然后嵌套一个for语句做这个限定组数的遍历,很重要的一点就是
while与for之间必须要归零sum,否则之前输入的那组数据会覆盖在sum身上,另外要注意下printf的位置
P5:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample Input
2
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include<stdio.h>
int main()
{
int a,b,c;
int sum=0;
scanf("%d",&c);
for(int k=1;k<=c;k++)
{
while(scanf("%d",&a)!=EOF&&a!=0)
{sum=0;
for(int i=1;i<=a;i++)
{
scanf("%d",&b);
sum+=b;
}
printf("%d\n",sum);
}
}
return 0;
} //P2+P4 的杂交体
P6:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.
Output
For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.
Sample Input
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d",&a)!=EOF)
{int sum=0;
for(int i=1;i<=a;i++)
{
scanf("%d",&b);
sum+=b;
}
printf("%d\n",sum);
}
} //P4的简化版
P7:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
Sample Input
1 5
10 20
Sample Output
6
30
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)==2)
printf("%d\n\n",a+b);
return 0;
} //只不过是P1多加一个\n罢了
P8:
-------------------------------------------------------------------------------------------------------------------------------------------------
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Sample Output
10
15
6
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include<stdio.h>
int main()
{
int a,b,c;
int sum=0;
scanf("%d",&c);
for(int k=1;k<=c;k++)
{
while(scanf("%d",&a)!=EOF&&a!=0)
{int sum=0;
for(int i=1;i<=a;i++)
{
scanf("%d",&b);
sum+=b;
}
if (k!=c)
printf("%d\n\n",sum);
else
printf("%d\n",sum);
}
}
return 0;
} //贴到OJ上PE,最后那一个不换行没实现,懒得搞了先放在这。
后来请教了Roni,做了一些修改:
int main()
{
int a,b,c,k,i;
int sum=0;
scanf("%d",&c);
for(k=1;k<=c;k++)
{
scanf("%d",&a);
for(i=1,sum=0;i<=a;i++)
{
scanf("%d",&b);
sum+=b;
}
if (k!=c)
printf("%d\n\n",sum);
else
printf("%d\n",sum);
}
return 0;
}
P8的输出是真的恶心。