day4 UVA 424

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
分析:就是高精度加法,實現多個數相加;
以char類型數組進行輸入,接着轉化爲int 類型;

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
using namespace std;
#include<cstring>
int main()
{
	int kk=101;
	int a[105];
	char b[105];
	int l;
	for(int i=0;i<105;i++)//初始化數組
		a[i]=0;
	while(cin>>b)
		
		{
			if(strcmp(b,"0")==0)
			break;
			l=strlen(b);
			for(int i=0;i<l;i++)//倒序計算,並轉換類型
			{
				a[i]=a[i]+b[l-i-1]-'0';
				
			}
			for(int i=0;i<=105;i++)//判斷進位
			{
				if(a[i]>=10)
				{
					a[i+1]=a[i+1]+a[i]/10;
					a[i]=a[i]%10;
					
				}
			}
	}
	while(a[kk]==0)//減少0的輸出
		kk--;
	for(int i=kk;i>=0;i--)
		cout<<a[i];
	cout<<endl;

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章