poj1503

Integer Inquiry
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24661   Accepted: 9578

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



这个一个让我非常蛋疼的一个问题,wrong answer了很多次,只是因为可能存在0123的情况,这真是故意为难人。必须得加上a[1]=='\0'


思路如下:首先输入字符串并且将字符串反转,然后加到之前的和上。最后将和反转便得到了结果。。。。

#include<iostream>
#include<string.h>
#include<stdio.h>

using namespace std;

int revert(char*a)
{
	int count=0;
	while(a[count]!='\0')count++;
	int i=0;
	int j=count-1;
	while(i<j)
	{
		char c=a[i];
		a[i]=a[j];
		a[j]=c;
		i++;
		j--;
	}
	return count;
}

int getsum(char  *sum,char  *a,int counta,int countsum)
{
	int s;
	int jinwei=0;
	if(counta>countsum)
	{
		for(int i=0;i<countsum;i++)
		{
			s=sum[i]-'0'+a[i]-'0'+jinwei;
			sum[i]='0'+s%10;
			jinwei=s/10;
		}
		for(int i=countsum;i<counta;i++)
		{
			s=a[i]-'0'+jinwei;
			sum[i]='0'+s%10;
			jinwei=s/10;
		}
		if(jinwei>0)
		{
			sum[counta]='0'+jinwei;
			sum[++counta]='\0';
		}
		return counta;
	}
	else
	{
		for(int i=0;i<counta;i++)
		{
			s=sum[i]-'0'+a[i]-'0'+jinwei;
			sum[i]='0'+s%10;
			jinwei=s/10;
		}
		for(int i=counta;i<countsum;i++)
		{
			s=sum[i]-'0'+jinwei;
			sum[i]='0'+s%10;
			jinwei=s/10;
		}
		if(jinwei>0)
		{
			sum[countsum]='0'+jinwei;
			sum[++countsum]='\0';
		}
    	return 	countsum;
	}
}


void deal()
{
	char sum[11001];
	char a[111];
	int counta;
	int countsum=0;
	for(int i=0;i<=11000;i++)sum[i]='\0';
	while(1)
	{
		cin>>a;
		if(a[0]=='0'&&a[1]=='\0')break;//注意判断是不是只有一个字符0
		counta=revert(a);
		countsum=getsum(sum,a,counta,countsum);
	}
	revert(sum);
	cout<<sum<<endl;
}

	

int main()
{
    deal();
    return 0;
}


你可能感兴趣的:(poj1503)