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

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 条回复
切换为时间正序
请发表友善的回复…
发表回复
昨日凡阳 2012-03-13
  • 打赏
  • 举报
回复
http://blog.csdn.net/fenglibing/article/details/1756773
freeCodeSunny 2008-03-22
  • 打赏
  • 举报
回复
上面要把A改成Main
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();
sum=sum.add(num1);
sum=sum.add(num2);
System.out.println(sum.toString());

}
}
yeshengyu 2008-03-22
  • 打赏
  • 举报
回复
啊?你用的是JAVA啊,我学的是C语言,呵呵
giftfish 2008-03-21
  • 打赏
  • 举报
回复
用数组
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;
}
星羽 2008-03-21
  • 打赏
  • 举报
回复
google 大数运算
ryfdizuo 2008-03-21
  • 打赏
  • 举报
回复
用int64_就可以了,baidu一下
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-21 09:13
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下