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時,結束測試。
{
//測試代碼。
}