hdu-1047Integer Inquiry

来源:互联网 发布:向多个gsm模块发数据 编辑:程序博客网 时间:2024/05/16 00:34

大数加法(模拟)

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9862    Accepted Submission(s): 2550


Problem Description
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 
 

Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself.
 

Output
Your program should output the sum of the VeryLongIntegers given in the input. 


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 

Sample Input
11234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900
 

Sample Output
370370367037037036703703703670
 
题目大意:给一些数字,然后求和。每组数据的结束标志是输入0。如果有多组数据,则每组数据之间用一个空行隔开。
注意这个输出格式,坑死无数好汉啊.....
#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#define maxn 110using namespace std;char str[maxn];int sum[maxn];void add(){    int len=strlen(str);    strrev(str);    int i,j,carry;    for(i=0,carry=0;i<len;i++)    {        sum[i]+=(str[i]-'0')+carry;        carry=sum[i]/10;        sum[i]%=10;    }    for(j=len;j<maxn;j++)//在这里还出了点问题,之前没有这一段,导致wa了几遍shit!。这里的作用就是防止相加的数字长度不一样,要补齐长度,才能正确的相加。    {        sum[j]+=carry;        carry=sum[j]/10;        sum[j]%=10;    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(sum,0,sizeof(sum));        while(scanf("%s",str)&&strcmp(str,"0"))        {            add();        }        int i,j;        for(i=maxn-1;i>0;i--)//注意这里的i=maxn-1,之前粗心,写成了i=maxn,成功贡献一次wa            if(sum[i])                break;        for(j=i;j>=0;j--)            printf("%d",sum[j]);        printf("\n");        if(t)            printf("\n");//注意格式,输出格式害死人啊    }    return 0;}



原创粉丝点击