A + B Problem II
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 156 Accepted Submission(s) : 61
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should
not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the
equation. Output a blank line between two test cases.
Sample Input
2 1 2 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
uC/C++中的int类型能表示的范围是-2E31-2E31–1。unsigned类型能表示的范围是0-2E32–1,即 0-4294967295。所以,int和unsigned类型变量,都不能保存超过10位的整数。有时我们需要参与运算的数,可能会远远不止10 位,例如,可能需要保留小数点后面100位(比如求π的值),那么,即便使用能表示很大数值范围的double变量,但是由于double变量只有64位,所以还是不可能达到精确到小数点后面100位这样的精度。
double变量的精度也不足以表示一个100位的整数。一般我们称这种基本数据类型无法表示的整数为大整数。如何表示和存放大整数呢?基本的思想就是:用数组存放和表示大整数。一个数组元素,存放大整数中的一位。
大数相加模板。
<span style="color:#333333;">#include <stdio.h>
#include <string.h>
#define MAX_LEN 200
int an1[MAX_LEN+10];
int an2[MAX_LEN+10];
char szLine1[MAX_LEN+10];
char szLine2[MAX_LEN+10];
int main(void)
{
scanf("%s", szLine1);
scanf("%s", szLine2);
int i, j;
memset( an1, 0, sizeof(an1));
memset( an2, 0, sizeof(an2));
int nLen1 = strlen( szLine1);
for( j = 0, i = nLen1 - 1;i >= 0 ; i --)
an1[j++] = szLine1[i] - '0';
int nLen2 = strlen(szLine2);
for( j = 0, i = nLen2 - 1;i >= 0 ; i --)
an2[j++] = szLine2[i] - '0';
for( i = 0;i < MAX_LEN ; i ++ )
{ an1[i] += an2[i]; //ÖðλÏà¼Ó
if( an1[i] >= 10 )
{ //¿´ÊÇ·ñÒª½øÎ»
an1[i] -= 10;
an1[i+1] ++; //½øÎ»
}
}
for( i = MAX_LEN; (i >= 0) && (an1[i] == 0); i -- ) ;
if(i>=0)
for( ; i >= 0; i--)
printf("%d", an1[i]);
else printf("0");
return 0;
}
</span>
下面是杭电的大数相加,注意格式
<span style="color:#000000;">#include<stdio.h>
#include<string.h>
#define max 10000//数组既然定义到外面就要开大点至少一万以上
int a[max+10];
int b[max+10] ;
char c[max+10];
char d[max+10];
int main(void)
{
int i,j,l1,l2,n,f=0;
scanf("%d",&n);
while(n--){
l1=0;l2=0;
scanf("%s",c);
scanf("%s",d);
l1=strlen(c);
l2=strlen(d);
memset(a,0,sizeof(a));//数组一定要清零
memset(b,0,sizeof(b));
for(j=0,i=l1-1;i>=0;i--)//把字符型数组变成一个个数逆向放到整型数组里;
{
a[j++]=c[i]-'0';
}
for(j=0,i=l2-1;i>=0;i--)
{
b[j++]=d[i]-'0';
}
for(i=0;i<max;i++)//把二个数组的每一位相加,记得进位 ,加到max 存到数组a中。得到的数前面有好多0
{
a[i]=a[i]+b[i];
if(a[i]>=10)
{
a[i]=a[i]-10;
a[i+1]+=1;
}
}
printf("Case %d:\n",++f);//输出case:
printf("%s + %s = ",c,d);//注意格式
for(i=max;(i>=0)&&(a[i]==0);i--);逆序减0至得到的大数和
if(i>=0)
{
for(;i>=0;i--)
printf("%d",a[i]);一个个输出
}
else printf("0");如果都是零则i=-1
if(n!=0)
printf("\n\n");
else
printf("\n");}
return 0;
}</span>