poj 1503 Integer Inquiry

Integer Inquiry

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 22014
Accepted: 8600

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

 

  1: #include<iostream>
  2: using namespace std;
  3: const int maxsize=120;
  4: int main()
  5: {
  6: 	int sum[maxsize]={0};
  7: 	int i,j,k;
  8: 	char temp[maxsize]={'\0'};
  9: 	int num[maxsize]={0};
 10: 	int a,b,c;
 11: 	int jin;
 12: 	int mark;
 13: 	while(1)
 14: 	{
 15: 		cin>>temp;
 16: 		if(strcmp(temp,"0")==0)  
 17:             break; 
 18: 		j=0;
 19: 		for(i=0;i<maxsize;i++)
 20: 			num[i]=0;
 21: 		for(i=strlen(temp)-1;i>=0;i--)
 22: 		{
 23: 			if(temp[i]>='0'&&temp[i]<='9')
 24: 				num[j]=temp[i]-'0';
 25: 			j++;
 26: 		}
 27: 		jin=0;
 28: 		for(k=0;k<maxsize;k++)
 29: 		{
 30: 			a=num[k];
 31: 			b=sum[k];
 32: 			c=a+b+jin;
 33: 			jin=c/10;
 34: 			sum[k]=c%10;
 35: 		}
 36: 	}
 37: 	mark=0;
 38: 	for(i=maxsize-1;i>=0;i--)
 39: 	{
 40: 		if(sum[i]!=0)
 41: 		{
 42: 			mark=i;
 43: 			break;
 44: 		}
 45: 	}
 46: 	if(mark!=0)
 47: 	{
 48: 		for(i=mark;i>=0;i--)
 49: 		{
 50: 			cout<<sum[i];
 51: 		}
 52: 	}
 53: 	cout<<endl;
 54: 	return 0;
 55: }
 56: 
posted @ 2011-11-21 19:52  w0w0  阅读(154)  评论(0)    收藏  举报