我这个新手在解ACM的题目时 关于按照题目要求的格式很是头疼,下面总结几点格式输入的问题
小结要点:
- 看题目是否有个N组数据,如果给了N,那么就要用while或for循环N次
- 一般用scanf函数输入那些具体的数字
- 题目要求:输入多组数据,但没有说输入几组。
- 输入形式: while(scanf("%d%d",&a,&b) != EOF) //因为题目有要求用EOF来结束输入
- 举个栗子:HDU1089
根据输入的内容计算出A + B的值。
注意:本题的输入以EOF结束
注意:本题有多组数据。
输入
输入由一系列成对的由空格分割的整数a和b构成,一对输入占用一行。
输出
对于任意一组输入的整数a和b,你应当在一行内输出a和b的和。
两组输出之间需换行分割。
输入样例
1 5
10 20
输出样例
6
30
代码如下:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b) != EOF)
{
printf("%d\n",a+b);
}
}
- 题目要求:输入N组数据,具体了输入几组。
- 输入形式: while(N--) //控制输出了几组
- 举个栗子:HDU1090
-
计算a+b
Input
输入第一行是一个整数N,代表接下来N行会有N组样例输入。
Output
每组输出占一行。
Sample Input
2 1 5 10 20
Sample Output
6 30
- 代码如下:
#include<stdio.h>
int main()
{
int a,b;
int N;
scanf("%d", &N);
while(N--)
{
scanf("%d%d",&a,&b);//用循环一组一组的输入进来
printf("%d\n", a+b);
}
}
- 题目要求:输入几组没有要求 只当ab为0 0时结束输入。
- 输入形式: while(scanf("%d%d",&a,&b)) //格式输入ab
- 举个栗子:HDU1091
-
计算a+b
Input
有多组样例输入,以0 0代表输入结束。
Output
每组输出占一行
Sample Input
1 5 10 20 0 0
Sample Output
6 30
- 代码如下:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b))//加上!=EOF也可以的
{
if(a ==0 && b ==0)
{
break;
}
else
printf("%d\n",a+b);
}
}
-
- 题目要求:输入几组有要求 只当N为0时结束输入。
- 输入形式: while(scanf("%d",&N)) //格式输入N
- 举个栗子:HDU1092
-
你的任务是计算一些整数的和
Input
输入包含多组样例。
每组样例包含一个整数N,然后在同行有N个整数。
若N = 0,则表示输入结束,这个样例不需要被处理。Output
对于每组用例,你应当输出这组样例N个数字的和,每个输出占一行。
Sample Input
4 1 2 3 4 5 1 2 3 4 5 0
Sample Output
10 15
- 代码如下:
#include<stdio.h>
int main()
{
int N;
int n;
int sum;
while(scanf("%d",&N))
{
if (N == 0)
{
break;
}
sum = 0;
while (N--)
{
scanf("%d",&n);
sum += n;
}
printf("%d\n",sum);
}
}
- 题目要求:输入几组有要求
- 输入形式: while(scanf("%d",&N)) //格式输入N
- 举个栗子:HDU1093
请计算下面每行数的和。
Input
第一行有一个整数N,表示有N行数。接下来N行,每行第一个为整数M,表示本行后面有M个整数。
Output
对于输入的每一行数,请分别对应输出每一行数的和。
Sample Input
2
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
#include<stdio.h>
int main()
{
int N,M;
int n;
int sum;
int i;
scanf("%d",&N);
for (i = 0 ; i < N ; i++)
{
scanf("%d", &M);//不断地输入数字
sum = 0;
while(M--)
{
scanf("%d", &n);
sum += n;
}
printf("%d\n", sum);
}
}
-
- 题目要求:输入多组数据,但没有具体到几组,那就不管直接scanf输入进N。
- 输入形式:while(scanf("%d",&N)) //格式输入N
- 举个栗子:HDU1094
-
请计算下面每行数的和。
Input
输入包含多组数据,每组数据占一行。每行第一个数为整数N,后面有N个整数
Output
对于输入的每一行数,请分别对应输出每一行数的和。
Sample Input
4 1 2 3 4 5 1 2 3 4 5
Sample Output
10 15
- 代码如下:
#include<stdio.h>
int main()
{
int N,n;
int sum;
int i;
while (scanf("%d", &N))
{
sum = 0;
for (i = 0; i < N; i++)
{
scanf("%d", &n);
sum += n;
}
printf("%d\n", sum);
}
}
- 题目要求:输入多组数据,但没有具体到几组,那就不管直接scanf输入进a,b。
- 输入形式: while(scanf("%d%d",&a,&b) //格式输入a,b
- 举个栗子:HDU1095
-
你的任务是计算a+b。
Input
输入由一系列成对的由空格分割的整数a和b构成,一对输入占用一行。
Output
对于每组输入,要求在一行输出它们的和。并且你要保证每组输出之间有一空行。
Sample Input
1 5 10 20
Sample Output
6 30
- 代码如下:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!= EOF)
{
printf("%d\n\n",a+b);//转一下 空一行又转了一下
}
}
- 题目要求:输入多组数据,但没有具体到几组,那就不管直接scanf输入进a,b。
- 输入形式: while(scanf("%d%d",&a,&b) //格式输入a,b
- 举个栗子:HDU1096
-
你的任务是求许多数的和。
Input
第一行需要输入一个整数N,接下来会有N行。 每一行开始输入一个整数M,然后会有M个数在同一行。
Output
对于每组输入,要求在一行输出它们的和。并且你要保证每组输出之间有一空行。
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,M,n;
int i;
int sum;
scanf("%d", &N);
for (i = 0; i < N; i++)
{
scanf("%d",&M);
sum = 0;
while (M--)
{
scanf("%d",&n);
sum += n;
}
printf("%d\n", sum);
if(i < N-1)
{
printf("\n");//最后这个地方没有空行
}
}
}