主题:求助高手大数相加问题
在OJ上作题可是在VC++里已经成功了,但就是AC不了,不知为什么求助:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
我的答案:
#include<iostream>
using namespace std;
void main()
{int time,j;
cin>>time;
for(j=1;j<=time;j++){
int a[1000],b[1000],sum[1000],i,num1,num2,n;
for(i=0;i<=999;i++){ //数组初始化
a[i]=0;
b[i]=0;
sum[i]=0;
}
if(j==1) getchar();
for (i=1;(n=getchar())&&(n!=' ')&&(n!='\n');i++){
a[i]=n-'0';
num1=i;
}
for (i=1;(n=getchar())&&(n!=' ')&&(n!='\n');i++){
b[i]=n-'0';
num2=i;
}
if (num1==num2){ //开始进行判断,加法
for(i=num1;i>=1;i--){
sum[i]+=(a[i]+b[i])%10;
if(a[i]+b[i]>=10){
sum[i-1]+=1;
}
}
}
if (num1>num2){
for(i=num2;i>=1;i--){
sum[i+num1-num2]+=(a[i+num1-num2]+b[i])%10;
if(a[i+num1-num2]+b[i]>=10){
sum[i+num1-num2-1]+=1;
}
}
for(i=num1-num2;i>=1;i--){
sum[i]+=a[i];
if (sum[i]>=10){
sum[i-1]+=1;
sum[i]=sum[i]%10;
}
}
}
if (num2>num1){
for(i=num1;i>=1;i--){
sum[i+num2-num1]+=(b[i+num2-num1]+a[i])%10;
if(b[i+num2-num1]+a[i]>=10){
sum[i+num2-num1-1]+=1;
}
}
for(i=num2-num1;i>=1;i--){
sum[i]+=b[i];
if (sum[i]>=10){
sum[i-1]+=1;
sum[i]=sum[i]%10;
}
}
}
cout<<"Case "<<j<<':'<<'\n';
for(i=1;i<=num1;i++){
cout<<a[i];
}
cout<<'+';
for(i=1;i<=num2;i++){
cout<<b[i];
}
cout<<'=';
if(num1==num2){
if (sum[0]==1){
cout<<1;
}
for (i=1;i<=num1;i++){
cout<<sum[i];
}
}
if(num1>num2){ //开始输出
if(sum[0]==1){
cout<<1;
}
for(i=1;i<=num1;i++){
cout<<sum[i];
}
}
if(num2>num1){ //开始输出
if(sum[0]==1){
cout<<1;
}
for(i=1;i<=num2;i++){
cout<<sum[i];
}
}
if(j!=time) cout<<'\n'<<endl;
else cout<<endl;
}
}
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
我的答案:
#include<iostream>
using namespace std;
void main()
{int time,j;
cin>>time;
for(j=1;j<=time;j++){
int a[1000],b[1000],sum[1000],i,num1,num2,n;
for(i=0;i<=999;i++){ //数组初始化
a[i]=0;
b[i]=0;
sum[i]=0;
}
if(j==1) getchar();
for (i=1;(n=getchar())&&(n!=' ')&&(n!='\n');i++){
a[i]=n-'0';
num1=i;
}
for (i=1;(n=getchar())&&(n!=' ')&&(n!='\n');i++){
b[i]=n-'0';
num2=i;
}
if (num1==num2){ //开始进行判断,加法
for(i=num1;i>=1;i--){
sum[i]+=(a[i]+b[i])%10;
if(a[i]+b[i]>=10){
sum[i-1]+=1;
}
}
}
if (num1>num2){
for(i=num2;i>=1;i--){
sum[i+num1-num2]+=(a[i+num1-num2]+b[i])%10;
if(a[i+num1-num2]+b[i]>=10){
sum[i+num1-num2-1]+=1;
}
}
for(i=num1-num2;i>=1;i--){
sum[i]+=a[i];
if (sum[i]>=10){
sum[i-1]+=1;
sum[i]=sum[i]%10;
}
}
}
if (num2>num1){
for(i=num1;i>=1;i--){
sum[i+num2-num1]+=(b[i+num2-num1]+a[i])%10;
if(b[i+num2-num1]+a[i]>=10){
sum[i+num2-num1-1]+=1;
}
}
for(i=num2-num1;i>=1;i--){
sum[i]+=b[i];
if (sum[i]>=10){
sum[i-1]+=1;
sum[i]=sum[i]%10;
}
}
}
cout<<"Case "<<j<<':'<<'\n';
for(i=1;i<=num1;i++){
cout<<a[i];
}
cout<<'+';
for(i=1;i<=num2;i++){
cout<<b[i];
}
cout<<'=';
if(num1==num2){
if (sum[0]==1){
cout<<1;
}
for (i=1;i<=num1;i++){
cout<<sum[i];
}
}
if(num1>num2){ //开始输出
if(sum[0]==1){
cout<<1;
}
for(i=1;i<=num1;i++){
cout<<sum[i];
}
}
if(num2>num1){ //开始输出
if(sum[0]==1){
cout<<1;
}
for(i=1;i<=num2;i++){
cout<<sum[i];
}
}
if(j!=time) cout<<'\n'<<endl;
else cout<<endl;
}
}