输入格式:有多个case输入,直到文件结束
输出格式:一行一个结果
Problem Description
Your task is to Calculate a + b
Too easy! Of course! I specially designed the problem for acm beginners
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim
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
Author
lcy
Recommend
JGShining
#include <stdioh>
int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) != EOF ) //输入直到文件结尾
{
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}
HDOJ1090
输入格式:先输入有case数,再依次输入每个case
输出格式:一行一个结果
#include <stdioh>
Problem Description
Your task is to Calculate a + b
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
Author
lcy
Recommend
JGShining
int main()
{ int n,a,b;
scanf( "%d" , &n ); //输入的case数
while( n-- ) //控制输入
{ scanf( "%d%d" , &a , &b );
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}
HDOJ1091
输入格式:每行输入一组case,当case中的数据满足某种情况时退出
输出格式:一行一个结果
Problem Description
Your task is to Calculate a + b
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
Author
lcy
Recommend
JGShining
#include <stdioh>
int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) && (a||b) ) //输入直到满足a和b均为0结束
{
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}
HDOJ1092
输入格式:每组case前有一个控制输入个数的数,当这个数为0结束
输出格式:一行一个结果
#include <stdioh>
Problem Description
Your task is to Calculate the sum of some integers
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
Author
lcy
Recommend
JGShining
int main()
{
int n,sum;
while( scanf( "%d" , &n ) && n ) //每组case前有一个控制该组输入数据的数,为0结束
{
int x;
sum = 0;
while( n-- ) //控制该组输入个数
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
}
return 0;
}
HDOJ1093
输入格式:一开始有一个控制总的输入case的数,而每个case中又有一个控制该组输入数据的数
输出格式:一行一个结果
Problem Description
Your task is to calculate the sum of some integers
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
Author
lcy
5
#include <stdioh>
int main()
{
int casnum,n,sum;
scanf( "%d" , &casnum ); //控制总的输入case的数
while( casnum-- ) //控制总的输入个数
{
int x;
sum = 0;
scanf( "%d" , &n ); //每个case中控制该组输入个数
while( n-- )
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
}
return 0;
}
HDOJ1094
输入格式:总的case是输到文件结尾,每个case中的一开始要输入一个控制该组个数的数
输出格式:一行一个结果
Problem Description
Your task is to calculate the sum of some integers
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
6
#include <stdioh>
int main()
{
int n,sum;
while( scanf( "%d" , &n ) != EOF ) //输出到文件结尾
{
int x;
sum = 0;
while( n-- ) //控制该组输入个数
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
}
return 0;
}
HDOJ1095
输入格式:输入直到文件结束
输出格式:一行一个结果,结果输完后还有一个blank line
Problem Description
Your task is to Calculate a + b
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
7
#include <stdioh>
int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) != EOF ) //输入直到文件结束
{
printf( "%d\n\n" , a+b ); //一行一个结果,结果输完后还有一个回车
}
return 0;
}
HDOJ1096
输入格式:一开始输入总的case数,每组case一开始有控制该组输入个数的数
输出格式:一行一个结果,两个结果之间有一个回车,注意最后一个case的处理。
Problem Description
Your task is to calculate the sum of some integers
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
#include <stdioh>
int main()
{
int casnum,n,sum;
scanf( "%d" , &casnum ); //总的输入case数
while( casnum-- ) //控制输入组数
{
int x;
sum = 0;
scanf( "%d" , &n ); //控制每组的输入个数
while( n-- )
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
if( casnum ) printf( "\n" ); //两两结果之间有一个回车,最后一个结果后面没有
}
return 0;
}算法竞赛题目中,由于重定向,最后将会出现EOF,而EOF标志着输入的结束。
为了利用EOF,算法竞赛选手无需考虑到底是什么东西(事实上,很多算法选手到今天甚至不知道这是什么,因为语言和库上知识的匮乏不影响算法竞赛选手做题)。所以我们一般直接当做输入失败处理,利用scanf返回输入成功参数的性质
典型的方法是:
例如每组数据第一个要输入的参数是p,则程序框架会这么写:
while(scanf("%d", &p) == 1)
{
//这里是其他参数的输入和算法过程
}
当第一行是三个数你可能会这么做:
当然了,刚刚那种办法是可以的,但为了使程序更漂亮:
while(scanf("%d%d%d", &a, &b, &c) == 3)
如果会使用EOF:
scanf() != EOF
但是注意!虽然大多数编译器的eof是-1,但是保险起见不要写-1,因为EOF是impl-def的。
输入流对象由于重载了bool,更容易:
while(std::cin>>a>>b)即可。以下是一个示例程序,可以输入多组数据直到输入-1,对于每组数据,输出对应的斐波那契数列尾数:
c
#include <stdioh>
int main() {
int n;
while (1) {
printf("请输入一个整数:");
scanf("%d", &n);
if (n == -1) {
break;
}
int a = 0, b = 1, c;
for (int i = 1; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
printf("斐波那契数列第 %d 项为:%d\n", n, a);
}
return 0;
}
该程序使用了一个无限循环 while (1),并在每次循环开始时要求用户输入一个整数。如果输入的整数是 -1,则跳出循环。
在每次循环中,使用 a 和 b 两个变量来保存斐波那契数列中当前项的前两项,然后使用一个循环计算第 n 项,最后输出结果。
注意,斐波那契数列的第 0 项是 0,第 1 项是 1。因此,在计算第 n 项时,循环变量 i 应该从 1 开始循环。c语言中有一个标准输入函数,即:scanf函数,它可以读取输入的任意格式类型的数据。scanf函数也有返回值,返回类型为int类型,它返回成功读入的项目的个数。如果它没有读取任何项目(当它期望一个数字而您却键入了一个非数字字符串时就会发生这种情况,scanf()会返回0。当它检测到“文件结尾”(end
of
file)时,它返回EOF(EOF是在文件stdioh中定义的特殊值,一般#define指令把EOF的值定义为-1,我们可以理解为:#define
EOF
-1)。
c语言中,所有的输入函数都共用同一个输入缓冲区,我们从键盘键入数据时,其实是将输入写入缓冲区中,当我们按下回车键时,scanf()函数从缓冲区中读取输入,刷新缓冲区。想实现不确定个数的运算,最好不要用数组,应为数组毕竟有限,你可以这样,定义一个变量存数,一个变量记个数,一个存总和,一个存平均数。设定一个循环,按特殊键结束循环输入,每读入一个数,就让他累加,个数曾一,最后求出平均数,而且对你的源程序改动不大根据数据格式的。
一般这种输入多行数据,ACM里面很常见
常用的方式有两种
1 输入整行字符串
while(gets(s))
2 每行有固定格式。
比如 固定两个整型
while(scanf("%d%d", &a, &b) != EOF)多组数据与单个数据的输入的区别在于多组数据的逻辑组成,这不是输入函数能解决的问题。
简单的说,多组数据可以通过添加循环来实现,即在循环中放入单个输入,并设定循环次数。此方法可以解决一个数组的输入;如果再外加一层循环,则可解决一个矩阵中所有数据的输入。
要实现你所想的功能,需要对输入流程进行处理,即定义某个输入为内层循环的终止值,当输入此值时表明本行数据输入完成。而外层循环处理所需要的行数。
希望能给你提供帮助。
感觉n太大的话好像算不出来,要用BigInt
#include<stdioh>long fun(long x){
if(x==1) return 1;
else return fun(x-1)x;
}
int main(){
long n;
while(scanf("%ld",&n)!=EOF){
printf("%ld\n",fun(n));
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)