您的位置:首页 > 其它

1089-1096

2015-07-27 08:55 447 查看
杭电的几道a+b,控制格式的题,大概整理一下....

1089

数据会超过int,用long long 型。

/*
Sample Input
1 5
10 20

Sample Output
6
30
*/
#include<stdio.h>
int main()
{
long long a,b;
while(~scanf("%lld%lld",&a,&b))
{
printf("%lld\n"a+b);
}
return 0;
}


1090

事先给定需要测试的组数,用循环来判断是否终止

/*
Sample Input
2
1 5
10 20

Sample Output
6
30
*/
#include<stdio.h>
int main()
{
long long a,b;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",a+b);
}
return 0;
}


1091

考察的特殊终止的格式,需要在循环条件处加限定

/*
Sample Input
1 5
10 20
0 0

Sample Output
6
30

*/
#include<stdio.h>
int main()
{
long long a,b;
while(scanf("%lld%lld",&a,&b),(a||b))
{
printf("%lld\n",a+b);
}
return 0;
}


1092
输入一个数n,后边是n个数,求和,n是0的时候程序终止

/*
Sample Input
4 1 2 3 4
5 1 2 3 4 5
0

Sample Output
10
15

*/
#include<stdio.h>
int main()
{
int i,n;long long a,s;
while(scanf("%d",&n),n)
{
s=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
s+=a;
}
printf("%lld\n",s);
}
return 0;
}
1093

输入一个数字,代表一共几组测试

每组第一个数字 n ,代表计算n个数的和,后边是 n 个数。

/*
Sample Input
2
4 1 2 3 4
5 1 2 3 4 5

Sample Output
10
15

*/
#include<stdio.h>
int main()
{
long long a,s;int i,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
s=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
s+=a;
}
printf("%lld\n",s);
}
return 0;
}


1094

读取到文件结束(相当于无限组输入),每组第一个数代表需要计算几个数的和。

/*
Sample Input
4 1 2 3 4
5 1 2 3 4 5

Sample Output
10
15

*/
#include<stdio.h>
int main()
{
long long a,s;int i,n;
while(~scanf("%d",&n))
{
s=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
s+=a;
}
printf("%lld\n",s);
}
return 0;
}


1095

计算a+b,每组之后一个空行

/*
Sample Input
1 5
10 20

Sample Output
6

30

*/
#include<stdio.h>
int main()
{
long long a,b;
while(~scanf("%lld%lld",&a,&b))
{
printf("%lld\n\n",a+b);
}
return 0;
}


1096

输入一个数,代表有多少组测试数据,每组第一个数代表本组有几个数字,输出这几个数字的和

每两组数据之间有空行,最后一组数据后没有空行。

/*
Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output
10

15

6

*/
#include<stdio.h>
int main()
{
int n,i,m,a,b,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=0;
scanf("%d",&m);
while(m--)
{
scanf("%d",&a);
sum+=a;
}
printf("%d\n",sum);
if(i<n-1)
{
printf("\n");
}
}
return 0;
}


1089-1096,这几个题,考查的是基本的输入和输出格式,基本上把所有的竞赛格式包括完了,格式掌握了,剩下的就是学习解决问题的方法了.....

加油!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: