# 两个无穷大的数相加的问题

yeshengyu 2008-03-21 09:13:16

A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11377 Accepted Submission(s): 1904

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

Author
Ignatius.L

...全文
431 10 打赏 收藏 举报

10 条回复

• 打赏
• 举报

http://blog.csdn.net/fenglibing/article/details/1756773
freeCodeSunny 2008-03-22
• 打赏
• 举报

freeCodeSunny 2008-03-22
• 打赏
• 举报

import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class A{
public static void main(String[] args){
BigInteger sum=new BigInteger("0");
BigInteger num1,num2;
Scanner cin=new Scanner(System.in);
num1=cin.nextBigInteger();
num2=cin.nextBigInteger();
System.out.println(sum.toString());

}
}
yeshengyu 2008-03-22
• 打赏
• 举报

• 打赏
• 举报

bm1408 2008-03-21
• 打赏
• 举报

kilvdn 2008-03-21
• 打赏
• 举报

``````

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

int main(){
char a[1005],b[1005];
int kase,ase=1;;
scanf("%d",&kase);
while (kase--){

//    while (scanf("%s",a)!=EOF){
scanf ("%s%s",a,b);
if (ase>1)  printf("\n");
printf("Case %d:\n",ase++);

int la,lb;
la=strlen(a);
lb=strlen(b);
printf("%s + %s = ",a,b);
char c[2020];
int i,j,k=0,jw=0;
for (i=la-1,j=lb-1;i>=0&&j>=0;i--,j--){
c[k++]=(a[i]-'0'+b[j]-'0'+jw)%10+'0';
jw=(a[i]-'0'+b[j]-'0'+jw)/10;
}
if (i==-1){
for (j;j>=0;j--){
c[k++]=(b[j]-'0'+jw)%10+'0';
jw=(b[j]-'0'+jw)/10;
}
}
else {
for (i;i>=0;i--){
c[k++]=(a[i]-'0'+jw)%10+'0';
jw=(a[i]-'0'+jw)/10;
}
}
if(jw!=0)
printf ("%d",jw);
for (k-=1;k>=0;k--)
printf ("%d",c[k]-'0');
printf("\n");
}
return 0 ;
}

``````
ryfdizuo 2008-03-21
• 打赏
• 举报

``````#include<stdio.h>
#include<string.h>
int main()
{
char str1[1000],str2[1000];
int a[1000],b[1000],c[1001];
int N;
int m,n,i,j,s,x,k,h,t;
int flag=0;
int len1,len2,len;

scanf("%d",&N);
for(i=1;i<=N;++i)
{
scanf("%s %s",&str1,&str2);
printf("Case ");
printf("%d",i);
printf(":\n");
printf("%s + %s = ",str1,str2);

len1=strlen(str1);
len2=strlen(str2);

for(s=len1-1,m=0;s>=0;s--)
{
a[m]=str1[s]-'0';
m++;
}
for(j=len2-1,n=0;j>=0;j--)
{
b[n]=str2[j]-'0';
n++;
}
for(h=len1;h<1000;h++)
a[h]=0;
for(t=len2;t<1000;t++)
b[t]=0;
if(len1>len2)
len=len1;
else if(len1=len2)
len=len1;
else
len=len2;
for(s=0;s<len;s++)
{
x=a[s]+b[s]+flag;
if(x>=10)
{
c[s]=x%10;
flag=1;
}
else
{
c[s]=x;
flag=0;
}
}
if(flag==1)
printf("1");
for(k=len-1;k>=0;k--)
printf("%d",c[k]);
printf("\n");
if (i<N)
printf("\n");
}
return 0;
}``````

• 打赏
• 举报

ryfdizuo 2008-03-21
• 打赏
• 举报

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-21 09:13

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下