c语言如何输入多组数据
1. c语言 输入多行数据
#include <iostream>
#include <list>
using namespace std;
int main()
{
int a,b;
list<int> l;
while (scanf("%d%d",&a,&b)!=EOF)
{
l.push_back(a+b);
}
while (!l.empty())
{
cout<<l.front()<<endl;
l.pop_front();
}
}
需要用到list保存结果,因为n未知大小所以用list更合适。
如果是想以比较方便的方式输入数据,可以考虑把多行的数据存在文本文件中,每次使用这些数据的时候,可以直接从这个文件里面读取。
如果编译后生成的可执行文件名为a.exe,存放数据的文件为b.txt(和a.exe放在同一文件夹下),则再控制台(命令行提示符状态)输入: a < b.txt 即可。 其中的小于号<,就是输入重定向符号。
(1)c语言如何输入多组数据扩展阅读:
C语言包含的各种控制语句仅有9种,关键字也只有32 个,程序的编写要求不严格且以小写字母为主,对许多不必要的部分进行了精简。实际上,语句构成与硬件有关联的较少,且C语言本身不提供与硬件相关的输入输出、文件管理等功能,如需此类功能,需要通过配合编译系统所支持的各类库进行编程,故c语言拥有非常简洁的编译系统。
2. c语言中怎样输入多组数据
以这道题为例。
intmain(){
for(inta,b;scanf("%d%d",&a,&b){
}
}
for循环里写你的算法就行了
3. C语言中如何实现多组数据输入输出
仔细认真看看下面的会对你有帮助的,嘿嘿
输入格式:有多个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;
}
4. c语言中怎样输入多组数据 每组数据个数不确定 每组数据占一行
多组数据与单个数据的输入的区别在于多组数据的逻辑组成,这不是输入函数能解决的问题。
简单的说,多组数据可以通过添加循环来实现,即在循环中放入单个输入,并设定循环次数。此方法可以解决一个数组的输入;如果再外加一层循环,则可解决一个矩阵中所有数据的输入。
要实现你所想的功能,需要对输入流程进行处理,即定义某个输入为内层循环的终止值,当输入此值时表明本行数据输入完成。而外层循环处理所需要的行数。
希望能给你提供帮助。
5. 如何设计输入处理多组数据的c语言程序
方法一;自定义函数,参数传递多组数据变量地址。这样对原数据处理,原变量值就改变了。
方法二:直接将数据定义成全局变量。
方法三:定义数组或结构体,将处理后的多组数据作为返回值返回。
方法四:一个函数要处理不定个数的参数,按最大参数个数接收,约定空值,之后在函数内做判断。
6. C语言如何实现输入数据有多组,输入以0 0结束。
可以循环输入数据,当遇到输入的值均为0时结束输入。
以输入整型为例,代码如下:
int a,b; //用来存输入的数据。
while(1)
{
scanf("%d%d",&a,&b);//以0 0结束输入,所以输入时是以空白字符分隔的。
if(a == 0 && b == 0) break; //当输入的全为0,结束输入。
//在这里添加使用输入数据的代码。
}
7. C语言中,我想要输入多组数据,请问要怎么输入,不用EOF
不用EOF的一个比较简易的方法就是输入正是数据之前先说明数据量有多少.
不知道量的话可以自己约定一个结束符.
比如遇到#就表示结束.
8. c语言中,一次连续输入多组数据,并且最后连续输出多组结果,应该用哪种方法
用二维数组就可以实现一次连续输入多组数据。思路是嵌套循环,外层循环控制二维数组的行数(也就是第几组数据),内层循环控制这组数据中数据个数。
采用二维数组方法的有点在于,这种随机存取的数据结构方便查找和检索,但一定要注意这种方法不便于向已有数据中插入和删除数据。
9. 想要同时在c语言中输入多个数该怎么办
C的数组是不可以动态增长,如果你不想使用链表,可以参考下面方法。
1、用malloc分配一块空间,比如int* a = (int*) malloc( 10*sizeof(int) );
然后可以当成好像是数组一样使用,比如a[2] = 5;
2、然后你需要增长的时候,就用realloc( a, 20*sizeof(int))扩展空间。不过每一次扩展都会有一次拷贝,相当于分配一块新的空间,然后把原来的数据拷贝过去,所以数组大了以后,速度会很慢。
3、使用while(scanf("%d",&n)!=EOF){}语句,直达输入ctrl+z,结束输入,例如:
#include<stdio.h>
intmain()
{
inta;
while(scanf("%d",&a)!=<ahref="https://www..com/s?wd=EOF&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3En1nzPWTzrH01"target="_blank"class="-highlight">EOF</a>){
printf("输出:%d ",a);
}
return0;
}
/*
运行结果:
54
输出:54
5156
输出:5156
21
输出:21
^Z
*/
10. C语言如何实现输入多组数据测试
循环按照格式读入每组数据即可。
对于输入多组数据测试的情况,需要约定结束的类型,常用的有两种:
1 当读入数据为一组特定值时,结束测试。
比如每组2个整型数据,以空格分隔,当输入的两个数均为-1时,结束测试。代码可以写作:
inta,b;
while(1)
{
scanf("%d%d",&a,&b);
if(a==-1&&b==-1)break;//退出测试的条件。
//测试代码。
}
2 当读到EOF时,结束测试。
同样读入两个整型数据,以空格分隔,当读到EOF时结束测试。代码可以写作:
inta,b;
while(scanf("%d%d",&a,&b)!=EOF)//当出现EOF时,结束测试。
{
//测试代码。
}