高精度-Integer Inquiry

Integer Inquiry
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30106   Accepted: 11728

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.

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670
注意此题输入结束标志:输入的那一行有且只有一个0结束,不是输入的第一个字符为0为结束标志,我原先判断结束的标志是当输入的一行字符串中第一个为0时程序结束,为此WA了好几次,在不是结束行中有可能会出现第一个字符为0.故判断结束时不能单单判断第一个字符是否为0,而应进一步说明那一行只有一个字符并接为0;
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int a[1000]={0},b[205]={0};
char str[205];
int main()
{
	int len1;
	int j,i;
	gets(str);
	int len=strlen(str);
	for(i=0,j=len-1;j>=0;j--,i++)
		b[i]=str[j]-'0';
    while(1)
	{
		if(str[0]=='0'&&str[1]=='\0')//注意判断结束的标志
			break;
		int h=0;
		int m;
		
        for(j=0;j<len;j++)
        {
            m=a[j]+b[j]+h;
			a[j]=m%10;
            h=m/10;
            
        }
		while(h)
		{
			a[j]=h%10;
			len++;
			h/=10;
		}
		gets(str);
        len1=strlen(str);
		for(i=0,j=len1-1;j>=0;j--,i++)
			b[i]=str[j]-'0';
		
    }
	int p;
    for(p=len-1;p>=0;p--)
		cout<<a[p];
	cout<<endl;
	return 0;
    
    
}


你可能感兴趣的:(高精度-Integer Inquiry)