HDoj 1096 A+B for Input-Output Practice (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.
 
Sample Input
3 4 1 2 3 4 5 1 2 3 4 5 3 1 2 3
 
Sample Output
10 15 6
 
Author
lcy
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:   1095  1091  1094  1090  1092 
 
题目解析: 本道题主要考察对输出格式的设置,要求每行数据都要相隔一行,假设一共有n行要输出,则从第0个数到第n-2个数,在这一行输出后光标要回车一行,再回车一行,这样在输出的时候,两行数据就会相隔一行,而在输出最后一个数据的时候,光标要回车一行,(既不能不会车,也不能回车两行),在codeblocks中,控制台会有时间数据统计报告,一般在最后光标的下一行开始显示。所以在本题的效果就是最后一行数据然后一个空白行,再就是统计报告。这样才不会出现Presentation Error。
还要注意在codeblocks中不能给源程序文件命名为other,否则生成的exe文件运行出错,具体原因未知。
 
C语言代码如下:
#include<stdio.h>
#include<malloc.h>
int main()
{

    int n=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int s=0;
        int num=0;
        scanf("%d",&num);
        int *a=(int*)malloc(sizeof(int)*num);
        for(int j=0;j<num;j++)
        {
            scanf("%d",&a[j]);
            s+=a[j];
        }
        free(a);
        if(i==n-1)
            printf("%d\n",s);
        else
            printf("%d\n\n",s);
    }
}

猜你喜欢

转载自www.cnblogs.com/wzmm/p/12451531.html