C 标准输入输出的8个代码示例

示例1

/*
Description 
你的任务是计算a+b。这是为了acm初学者专门设计的题目。你肯定发现还有其他题目跟这道题的标题类似,这些问题也都是专门为初学者提供的。
Input 
输入包含一系列的a和b对,通过空格隔开。一对a和b占一行。
Output 
对于输入的每对a和b,你需要依次输出a、b的和。 
如对于输入中的第二对a和b,在输出中它们的和应该也在第二行。

Sample Input Copy 
1 5
10 20
Sample Output Copy 
6
30
*/

#include <stdio.h>
int main(void)
{
    int a, b;
    while (scanf("%d %d", &a, &b) != EOF)
        printf("%d\n", a + b);
    return 0;
}

示例2

/*
Description 
你的任务是计算a+b。 
Input 
第一行是一个整数N,表示后面会有N行a和b,通过空格隔开。 
Output 
对于输入的每对a和b,你需要在相应的行输出a、b的和。 
如第二对a和b,对应的和也输出在第二行。 

Sample Input Copy 
2
1 5
10 20
Sample Output Copy 
6
30
*/

#include <stdio.h>
int main(void){
    int a,b,n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d %d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;  
}

 示例3

/*
Description 
你的任务是计算a+b。 
Input 
输入中每行是一对a和b。其中会有一对是0和0标志着输入结束,且这一对不要计算。
Output 
对于输入的每对a和b,你需要在相应的行输出a、b的和。 
如第二对a和b,他们的和也输出在第二行。 

Sample Input Copy 
1 5
10 20
0 0
Sample Output Copy 
6
30
*/

#include <stdio.h>
int main(void) {
    int a,b;
    while(scanf("%d %d",&a,&b)){
        if(a==0&&b==0)
            break;
        printf("%d\n",a+b);
    } 
    return 0;
}

示例4

/*
Description 
你的任务是计算若干整数的和。 
Input 
每行的第一个数N,表示本行后面有N个数。 
如果N=0时,表示输入结束,且这一行不要计算。
Output 
对于每一行数据需要在相应的行输出和。

Sample Input Copy 
4 1 2 3 4
5 1 2 3 4 5
0 
Sample Output Copy 
10
15
*/

#include <stdio.h>
int main(void){
    int n,tmp,stmp;
    while(~scanf("%d",&n)&&n!=0){
        tmp=0;
        for(int i=0;i<n;i++){
            scanf("%d",&stmp);
            tmp+=stmp;
        }
        printf("%d\n",tmp);
    }
    return 0;
}

示例5

/*
Description 
你的任务是计算若干整数的和。 
Input 
输入的第一行是一个正数N,表示后面有N行。每一行的第一个数是M,表示本行后面还有M个数。
Output 
对于每一行数据需要在相应的行输出和。 

Sample Input Copy 
2
4 1 2 3 4
5 1 2 3 4 5
Sample Output Copy 
10
15
*/

#include <stdio.h>
int main(void){
    int t,n,tmp,stmp;
    scanf("%d",&t);
    while(t--){
    scanf("%d",&n);
        tmp=0;
        for(int i=0;i<n;i++){
            scanf("%d",&stmp);
            tmp+=stmp;
        }
        printf("%d\n",tmp);
    }
    return 0;
}

示例6

/*
Description 
你的任务是计算若干整数的和。 
Input 
每行的第一个数N,表示本行后面有N个数。 
Output 
对于每一行数据需要在相应的行输出和。 

Sample Input Copy 
4 1 2 3 4
5 1 2 3 4 5
Sample Output Copy 
10
15
*/

#include <stdio.h>
int main(){
    int n,x;
    while(scanf("%d",&n)!=EOF){
        int res=0;
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            res+=x;
        }
        printf("%d\n",res);
    }
    return 0;
}

示例7

/*
Description 
你的任务是计算两个整数的和。 
Input 
输入包含若干行,每行输入两个整数a和b,由空格分隔。 
Output 
对于每组输入,输出a和b的和,每行输出后接一个空行。 

Sample Input Copy 
1 5
10 20
Sample Output Copy 
6

30

*/

#include <stdio.h>
int main(){
    int a,b,res;
    while(scanf("%d %d",&a,&b)!=EOF){
        res=a+b;
        printf("%d\n\n",res);
    }
    return 0;
}

示例8

/*
Description 
你的任务是计算若干整数的和。 
Input 
输入的第一行为一个整数N,接下来N行每行先输入一个整数M,然后在同一行内输入M个整数。 
Output 
对于每组输入,输出M个数的和,每组输出之间输出一个空行。 
Sample Input Copy 
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Sample Output Copy 
10

15

6
*/

#include <stdio.h>
int main(){
    int n,m,x,res;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&m);
        res=0;
        for(int j=0;j<m;j++){
            scanf("%d",&x);
            res+=x;
        }
        printf("%d\n\n",res);
    }
    return 0;
}
发布了7 篇原创文章 · 获赞 1 · 访问量 116

猜你喜欢

转载自blog.csdn.net/qq_45599068/article/details/104083018
今日推荐