Online Judge计算整数的和

来源:互联网 发布:sql create table as 编辑:程序博客网 时间:2024/06/05 14:44
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
34 1 2 3 45 1 2 3 4 53 1 2 3
 

Sample Output
10156我的代码如下:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>int main(int argc,char **argv){    int row; //将要输入的行    int rowmax;//每行输入整数个数    scanf("%d",&row);    int *pt[row];    int result[row];    memset(result,0,sizeof(int) * row);    int i=row;    for(;row>0;row--){        scanf("%d",&rowmax);        pt[row-1]=(int *)malloc(rowmax * sizeof(int));        if(pt[row-1]==NULL){            perror(strerror(errno));        }        else{            for(;rowmax>0;rowmax--){                scanf("%d",pt[row-1]);                result[row-1]+=*pt[row-1];                pt[row-1]++;            }        }    }           for(;i>0;i--)        printf("%d\n\n",result[i-1]);    return 0;}注:我动态申请的空间用完后没有释放,因不知指针数组空间如何释放。下面代码是别人所写,拿来对比:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
 
int main (int argc,char*argv[])
{   
    int row;
    int i,j=0;
    char num;
    int* result=NULL;
    printf("Enter the row: ");
    scanf("%d",&row);
    result = malloc(row*sizeof(int));
    memset(result,0,sizeof(result));
    getchar();
    for (i=0;i<row;i++)
    {
        result[i] = 0;
        num = getchar();
        while(1)
        {
            num = getchar();
            if (num == '\n')
            {
                break;
            }
            else if (num != ' ')
            {
                result[i]+=(num-0x30);
            }
        }
    }
    for (i=0;i<row;i++)
    {
        printf("%d\n\n",result[i]);
    }
    return 0;
}
原创粉丝点击