A+B ProblemII(高精度)
3682 ワード
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 188211 Accepted Submission(s): 35956
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
タイトル:
T(1~20)個のcaseを与え、後に2個の数a,bを与え、各数の長さは最大1000ビットであり、2個の数加算と要求フォーマットで出力を求める.
考え方:
入力の順序は上位からポジションへ入力するので,計算時にn->0から計算し,キャリーケースを考慮する.数の入出力を考慮すると、char型配列を開くのが便利で、計算するときは文字0を減算すればよい.
2つの数の長さが一致しない可能性があるので、まずそれに対して、前に0を補ってから計算して、出力する時にプリアンブル0を出力しないで、各caseの間に1行空けて、最後のcaseは空の行を必要としないで、たぶんこれらに注意すればACができます.
AC:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 188211 Accepted Submission(s): 35956
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
タイトル:
T(1~20)個のcaseを与え、後に2個の数a,bを与え、各数の長さは最大1000ビットであり、2個の数加算と要求フォーマットで出力を求める.
考え方:
入力の順序は上位からポジションへ入力するので,計算時にn->0から計算し,キャリーケースを考慮する.数の入出力を考慮すると、char型配列を開くのが便利で、計算するときは文字0を減算すればよい.
2つの数の長さが一致しない可能性があるので、まずそれに対して、前に0を補ってから計算して、出力する時にプリアンブル0を出力しないで、各caseの間に1行空けて、最後のcaseは空の行を必要としないで、たぶんこれらに注意すればACができます.
AC:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,time = 0;
scanf("%d",&n);
while(n--)
{
int CIN = 0,len_a,len_b,len_sum;
char a[1005],b[1005],sum[1005],temp[1005],temp_a[1005],temp_b[1005];
time++;
scanf("%s%s",a,b);
len_a = strlen(a);
len_b = strlen(b);
len_sum = max(len_a,len_b);
strcpy(temp_a,a);
strcpy(temp_b,b);
if(len_a > len_b)
{
int idex_b = 0,i;
for(i = 0;i < len_a - len_b;i++)
temp[i] = '0';
for(i;i < len_a;i++,idex_b++)
temp[i] = b[idex_b];
for(i = 0;i < len_a;i++)
b[i] = temp[i];
b[i] = '\0';
}
if(len_b > len_a)
{
int idex_a = 0,i;
for(i = 0;i < len_b - len_a;i++)
temp[i] = '0';
for(i;i < len_b;i++,idex_a++)
temp[i] = a[idex_a];
for(int i = 0;i < len_b;i++)
a[i] = temp[i];
a[i] = '\0';
}
for(int i = len_sum - 1;i >= 0;i--)
{
int ans = (a[i] + b[i] - '0' - '0' + CIN);
sum[i] = ans % 10 + '0';
CIN = ans / 10;
}
sum[len_sum] = '\0';
printf("Case %d:
",time);
printf("%s + %s = ",temp_a,temp_b);
if(CIN) printf("%d",CIN);
puts(sum);
if(n) printf("
");
}
return 0;
}