UVA 424 - Integer Inquiry

Integer Inquiry

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).

Output

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

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

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output 

370370367037037036703703703670
本題呢就是簡單的大整數加法,不過由於本人很水,所以還是調了挺久,主要是犯了一些低級錯誤

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100000 
void add(char *s1,char *s2)
{
	int l1,l2;
	int a[MAX],b[MAX];
	memset(a,0,sizeof(0));
	memset(b,0,sizeof(0));
	l1=strlen(s1);
	l2=strlen(s2);
	for(int i=l1-1;i>=0;i--)
		a[l1-i-1]=s1[i]-'0';
	for(int i=l2-1;i>=0;i--)
		b[l2-1-i]=s2[i]-'0';
		
	int l = l1 > l2 ? l1 : l2;
	int k;
	int c=0;
	for(k=0;k<l;k++){
		int tmp;
		tmp=a[k]+b[k]+c;
		a[k]=tmp%10;
		c=tmp/10;
	}
	if(c){
		a[k]=c;
		l++;
	}
	for(k=0;k<l;k++)
		s1[k]=a[l-1-k]+'0';
}
int main()
{
	char s1[MAX],s2[MAX];
	memset(s1,0,sizeof(s1));
	memset(s2,0,sizeof(s2));
	while(scanf("%s",s2) && strcmp(s2,"0")){
		add(s1,s2);
	}
	printf("%s\n",s1);
	return 0;
}



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