2011-12-14 06:08:05
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1002
题意:a+b,不过是大数的。1000位十进制。
mark:大数加法,不是很难写。1Y。
代码:
# include <stdio.h>
# include <string.h>
char s1[1010], s2[1010], s3[1010] ;
int a[1010], b[1010], buff[1010] ;
void strtonum (char s[], int num[])
{
int i ;
num[0] = strlen(s) ;
for (i = 1 ; i <= num[0] ; i++)
num[i] = s[num[0]-i]-'0' ;
}
void numtostr(int num[], char s[])
{
int i ;
for (i = 0 ; i < num[0] ; i++)
s[i] = num[num[0]-i] + '0' ;
s[num[0]] = '\0' ;
}
void add(int a[], int b[], int c[])
{
int i, *p, *q, cc = 0 ;
if (a[0] < b[0]) p = a, q = b ;
else p = b, q = a ;
for (i = 1 ; i<= q[0] ; i++)
{
if (i <= p[0]) buff[i] = p[i] ;
else buff[i] = 0 ;
buff[i] += q[i]+cc ;
cc = buff[i] / 10 ;
buff[i] %= 10 ;
}
if (cc != 0) buff[i++] = cc ;
buff[0] = i-1 ;
for (i = 0; i <= buff[0] ; i++)
c[i] = buff[i] ;
}
int main()
{
int T, nCase = 1 ;
scanf ("%d", &T) ;
getchar () ;
while (T--)
{
scanf ("%s %s", s1, s2) ;
strtonum(s1,a) ;
strtonum(s2,b) ;
add(a,b,a) ;
numtostr(a,s3) ;
if (nCase != 1) printf ("\n") ;
printf ("Case %d:\n%s + %s = %s\n", nCase++, s1,s2,s3) ;
}
return 0 ;
}