『壹』 怎麼用c語言通過兩個變數一次輸出兩組和的值
1.用for循環
for(int i=1;i《=2;++i)
{scanf(。。。。)
printf(「%d」,a+b);
}
2.利用scanf的返回值進行不知道幾組的相加
while(scnaf(。。)!=EOF)
printf(「%d
」,a+b);
『貳』 急!!!C語言:輸出兩組數組中相同的數字
//示例代碼如下
#include<stdio.h>
#include<stdlib.h>
intmain()
{
intm,n,i,j,flag=0;
int*pm,*pn;
scanf("%d%d",&n,&m);
pm=(int*)malloc(sizeof(int)*m);
pn=(int*)malloc(sizeof(int)*n);
for(i=0;i<m;i++)
scanf("%d",pm+i);
for(i=0;i<n;i++)
scanf("%d",pn+i);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
if(pm[i]==pn[j]){
printf("%d",pn[j]);
flag=1;
}
}
if(!flag)
printf("NO ");
printf(" ");
free(pm);
free(pn);
return0;
}
//示例運行結果
F:c_work>a.exe
57
5917123698
789161792698
917698
『叄』 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;
}
『肆』 c語言 輸入兩組數,一組數內可能有重復的數,輸出兩組數交集,求大神幫我看看該怎麼改
有辦法,首先,先把兩個數組從小到大排序,然後用兩個指針分別指向兩個數組。然後,對兩個數組分別重頭進行比較,然後再申請第三個數組,如果說第一個數組較小,則賦值給第三個,分別遍歷兩個數組,相等的情況就只取一個,就可以了,簡單的數據結構。
『伍』 在C語言里,printf怎麼輸出2個變數
%d是整數的輸出標識符。要輸出兩個變數,用逗號隔開即可。標識符和變數的順序是相對應的,不能錯。輸出2個變數的代碼修改:
{int a,b,c,sum,sum1;
a=10;
b=20;
c=30;
sum=a+b;
sum1=a+b+c;
printf ("sum=%d/nsum1=%d/n",sum,sum1);}
(5)c語言輸出兩組數據怎麼處理擴展閱讀:
變數可以通過變數名訪問。在指令式語言中,變數通常是可變的;但在純函數式語言(如Haskell)中,變數可能是不可變(immutable)的。
在一些語言中,變數可能被明確為是能表示可變狀態、具有存儲空間的抽象(如在Java和Visual Basic中);但另外一些語言可能使用其它概念(如C的對象)來指稱這種抽象,而不嚴格地定義「變數」的准確外延。
如果按存儲佔用空間來分,變數可以是整型變數,字元型變數,浮點型變數等。當然還有數組,結構體變數等。
變數的命名規則:
變數名必須以字母或下劃線 "_" 開頭。
變數名只能包含字母數字字元以及下劃線。
變數名不能包含空格。如果變數名由多個單片語成,那麼應該使用下劃線進行分隔(比如 $my_string),或者以大寫字母開頭(比如 $myString)。
『陸』 C語言中如何實現多組數據輸入輸出
C語言中實現多組數據輸入輸出主要有兩種方式:
1.首先輸入一個n,表示將有n個輸入輸出,例如:
#include <stdio.h>
int main()
{
int n,a;
scanf("%d",&n);
while(n--){
scanf("%d",&a);
printf("輸出:%d\n",a);
}
return 0;
}
/*
運行結果:
3
255
輸出:255
156
輸出:156
125
輸出:125
*/2.使用while(scanf("%d",&n)!=EOF){}語句,直達輸入ctrl+z,結束輸入,例如:
#include <stdio.h>
int main()
{
int a;
while(scanf("%d",&a)!=EOF){
printf("輸出:%d\n",a);
}
return 0;
}
/*
運行結果:
54
輸出:54
5156
輸出:5156
21
輸出:21
^Z
*/
『柒』 C語言:以下程序是輸出26個大寫字母和它們的ASCII代碼,每行輸出兩組數據
26個字母,從0到25,正好26個,如果是《=,就是27個了,i只是個循環控制變數,控制循環的次數而已。
printf("c=%c ascii=%d",ch,ch) ,兩個都是ch,因為輸出的是同一個變數,只是兩個格式顯示而已,一個是字元類型%c,一個是整型%d。
『捌』 C語言中如何實現輸入輸出多組數據,該如何結束輸入
c語言中實現多組數據輸入輸出主要有兩種方式:
1.首先輸入一個n,表示將有n個輸入輸出,例如:
#include
int main()
{
int n,a;
scanf("%d",&n);
while(n--){
scanf("%d",&a);
printf("輸出:%d\n",a);
}
return 0;
}
/*
運行結果:
3
255
輸出:255
156
輸出:156
125
輸出:125
*/2.使用while(scanf("%d",&n)!=eof){}語句,直達輸入ctrl+z,結束輸入,例如:
#include
int main()
{
int a;
while(scanf("%d",&a)!=eof){
printf("輸出:%d\n",a);
}
return 0;
}
/*
運行結果:
54
輸出:54
5156
輸出:5156
21
輸出:21
^z
*/