C语言中如何实现多组数据输入输出?

 我来答
GJW2900491124
2016-10-27 · TA获得超过168个赞
知道答主
回答量:17
采纳率:0%
帮助的人:15.3万
展开全部
#include<stdio.h>
#include<math.h>

int main(){
size_t T = 0;
int i = 0,j = 0,x = 1;
printf(""); //提示输入测试数据的组数(即T),懒得写内容了,自己加上去吧233
scanf("%d",&T);

int n[T],k[T];
int sum[T];

for(; i<T; i++){
scanf("%d %d",&n[i],&k[i]);
if(n[i]<0){
printf("");  //提示输入的数中,n和k的取值范围为 0<=n<=10,1<=k<=8,printf的内容自己加上吧
return 0;
}
}


for( j = i,i=0; i<j; i++){
sum[i] = 0;
}

for( i=0; i<j; i++){
for(; x<=k[i];x++)
sum[i] += pow(n[i],x);
printf("\n%-4d",sum[i]);
}

return 0;
}


以上是程序,大概就是这样了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
创作者ortAhFHRGT
2019-07-11 · TA获得超过3.8万个赞
知道小有建树答主
回答量:1.2万
采纳率:25%
帮助的人:918万
展开全部
n--表示n = n-1,那么while(n--)的隐含条件是(while(n-- > 0)),所以会循环n次。
你也可以定义一个变量i;
int i;
for(i=0;i<n;i++){
//do something... ...
}
两种写法等价。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小千来玩
2018-05-23 · TA获得超过1050个赞
知道答主
回答量:0
采纳率:25%
帮助的人:0
展开全部

c语言中有一个标准输入函数,即:scanf函数,它可以读取输入的任意格式类型的数据。scanf函数也有返回值,返回类型为int类型,它返回成功读入的项目的个数。如果它没有读取任何项目(当它期望一个数字而您却键入了一个非数字字符串时就会发生这种情况,scanf()会返回0。当它检测到“文件结尾”(end of file)时,它返回EOF(EOF是在文件stdio.h中定义的特殊值,一般#define指令把EOF的值定义为-1,我们可以理解为:#define EOF -1)。 

c语言中,所有的输入函数都共用同一个输入缓冲区,我们从键盘键入数据时,其实是将输入写入缓冲区中,当我们按下回车键时,scanf()函数从缓冲区中读取输入,刷新缓冲区。

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
过鹭人18
2018-03-07 · TA获得超过2075个赞
知道小有建树答主
回答量:5
采纳率:100%
帮助的人:1213
展开全部

C语言中实现多组数据输入输出主要有两种方式:

1.首先输入一个n,表示将有n个输入输出,例如:

2.使用while(scanf("%d",&n)!=EOF){}语句,直达输入ctrl+z,结束输入,例如:

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
liyayaxixi
2017-12-25 · TA获得超过206个赞
知道答主
回答量:9
采纳率:0%
帮助的人:9.7万
展开全部
仔细认真看看下面的会对你有帮助的,嘿嘿

输入格式:有多个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 <stdio.h>

int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) != EOF ) //输入直到文件结尾
{
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}

HDOJ1090
输入格式:先输入有case数,再依次输入每个case
输出格式:一行一个结果
#include <stdio.h>
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 <stdio.h>

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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>

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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式