美文网首页程序员
A+B for Input-Output Practice (I

A+B for Input-Output Practice (I

作者: iamblackcat | 来源:发表于2018-12-08 20:22 被阅读3次

    V.

    图片.png

    题目的要求是我们首先输入一个整型数据N,然后是N行,每一行的第一个数字M代表本行有M个需要相加的数,考虑到我们并不能确定N的个数,即有多少行的数据需要相加,于是我们引入数组;作为一个系列的开端,深入理解它对后面的几道很有帮助,接下来逐行对源代码分析:

    #include<stdio.h>
    int main()
    {
        int n,i,j,m=0,su=0,sum[100],a;//定义变量与数组,注意到一些是初始化的,下面会有用;
        scanf("%d",&n);//接收键入的值作为题目中的N,存放在n中;
        while (m<n)//当m小于n时循环下去,对m初始化的意义所在;
        {
            scanf("%d",&i);//即每一行的第一个数据,存放在i中;
            for(j=1;j<=i;j++)//for循环,控制读入i个数据;
            {
                scanf("%d",&a);
                su = su + a;//当执行完for循环,su便是这i个数据的和;
            }
            sum[m] = su;//执行完for循环,su的值便被存入数组;
            m++;
            su = 0;//细节哦,执行一次后要对本次的su“初始化”,否则会影响下一组的结果;
        }
        m = 0;
        for(j=1;j<=n;j++,m++)//循环输出一个数组的每个数据;
        {
            printf("%d\n",sum[m]);//留心换行,格式很重要;
        }
        return 0;
    } 
    

    VI.

    图片.png

    感觉......这一道应该放在前面才对吧,跟第五道的不同在于不需要提前设定计算的组数,也就是说我们不需要再引入数组了;直接输入一行一行的数据,第一个数据代表本行需要相加的数据个数,敲下enter后返回本行的值并继续输入继续计算,继续输入,继续计算......

    #include<stdio.h>
    int main()
    {
        int n,i,j,m=0,sum=0,a;
        while (scanf("%d",&i)!=EOF)//还是"!=EOF"应对连续输入,连续计算的问题;
        {
            for(j=1;j<=i;j++)
            {
                scanf("%d",&a);
                sum = sum + a;
            }
            printf("%d\n",sum);
            m++;
            sum = 0;//细节细节......
        }
        return 0;
    }
    

    VII.

    Problem Description

    Your task is to Calculate a + b.

    The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

    Example Input

    1 5
    10 20

    Example Output

    6

    30

    看起来更简单了是吗?其实一开始我也这么想,没错哇,就是这样,要相信自己;

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

    VIII.

    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.

    Example Input

    3
    4 1 2 3 4
    5 1 2 3 4 5
    3 1 2 3

    Example Output

    10

    15

    6

    第八题了,也是困住我时间最长的一道,提交了有七八次,换了几个方案,刚开始是Wrong Answer,后来就一直是Presentation Error,很头大,实在是无语。说到底,是细节,注意输出的格式很重要;

    #include<stdio.h>
    int main()
    {
        int n,sum,m,i,a[100];//定义变量与数组;
        scanf("%d",&n);
        while(n--)//while循环,同时n做自减运算,技巧哦,当n自减到等于0的时候,因为C语言里,“0”代表“假”,于是退出循环;
        {
            sum=0;
            scanf("%d",&m);
            for(i=1;i<=m;i++)
            {
                scanf("%d",&a[i]);
                sum=sum+a[i];
            }
            if(n!=0)
                printf("%d\n\n",sum);//n不等于0的时候输出两个换行,
            if(n==0)
                printf("%d\n",sum);//n等于0,也意味着程序运行将要完毕了,只输出一个换行;
        }
    }
    

    后两个的时候加载不出来图片了,就把题目敲出来了;做完第八题才明白第七题的用意,第七题注意一下换行就好了,所以确实感觉很简单的样子,做到完第八题,才恍然大悟“原来是在给第八题打前站啊。。。”

    相关文章

      网友评论

        本文标题:A+B for Input-Output Practice (I

        本文链接:https://www.haomeiwen.com/subject/siaqhqtx.html