Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

HDOJ Problem 1002 - A + B Problem II

2019/09/15 ACM竞赛入门
Word count: 967 | Reading time: 5min

HDOJ Problem 1002 - A + B Problem II:

大数定理

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
1
2
3
2
1 2
112233445566778899 998877665544332211
Sample Output
1
2
3
4
5
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

解法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string.h>
using namespace std;
char a[1000],b[1000];
int aar[1001],bar[1001];

int main()
{
int alen,blen,maxlen;
int time;
cin >> time;
for (int i=1;i<=time;i++)
{
cin >> a >> b;
alen = strlen(a);
blen = strlen(b);
int tmp = 0;
for (int j=alen-1;j>=0;j--)
aar[tmp++] = a[j]-'0';
tmp = 0;
for (int j=blen-1;j>=0;j--)
bar[tmp++] = b[j]-'0';
//123 ==> 321 为了保证之后便于计算

if ( alen > blen){
maxlen = alen;
for(int j=blen;j<alen;j++){ //长度不同时,短的那个需要补零
bar[j] = 0;
}
aar[alen] = 0;
}
else if ( alen < blen){
maxlen = blen;
for(int j=alen;j<blen;j++)
{
aar[j] = 0;
}
bar[blen] = 0;
}
else{
maxlen = alen;
aar[maxlen]=0;
bar[maxlen]=0;
}

for (int j=0;j<maxlen;j++){
cout << aar[j] << '\t';
cout << bar[j] <<endl;
aar[j] += bar[j];

if(aar[j] >= 10){//如果当前位大于10则进一位
aar[j] -=10;
aar[j+1] += 1;
}
}//将a+b的和保存在aar数组里

cout << "Case:" << i<< endl;
cout << a << " + " << b << " = " ;

if (aar[maxlen] == 0 ) //判断第一位是否为0,如果是的话就从第二位开始读,这个是逆序
for (int j=maxlen-1;j>=0;j--) cout << aar[j];
else
for (int j=maxlen;j>=0;j--)
cout << aar[j];

if(i != time) //注意要求的输出格式
cout << endl<< endl;
else
cout << endl;
}
return 0;
}
解法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

int i,j,y,n,k,h,p,lena,lenb;
int a[1000],b[1000],sum[1000],f[1000];
int main()
{
string a1,b1;
cin>>n;
for(y=1;y<=n;y++)
{
cin>>a1>>b1;
lena=a1.length();
lenb=b1.length();
for(i=0;i<1000;i++){
a[i]=0;b[i]=0;f[i]=0;//f数组是记录a+b和
//这个补零是先全都设为0,再把不为0的填入
}
for(i=lena-1;i>=0;i--) /*1234 ==> 4321*/
a[lena-1-i]=a1[i]-'0'; //字符'9' - '0' 才是数字9
for(i=lenb-1;i>=0;i--)
b[lenb-1-i]=b1[i]-'0';
k=0;
for(i=0;i<lenb || i<lena;i++){ //i--> max( lena , lenb )
h=a[i]+b[i]+k; //k是下一位是否进一
f[i]=h%10; //f[i]必然是0-9
k=h/10; //如果h大于10,则k=1,如果h小于10,则k=0
}
if(k!=0) //如果k=1,则最高位加一
f[i++]=k;
cout<<"Case "<<y<<":"<<endl<< a1 <<" + "<< b1 <<" = ";
p=0;
for(j=i-1;j>=0;j--){ //将之前为了计算时的倒序,再反正过来
if(p==0 && f[j]==0){ //目的是去前导0,实则这步多余了
continue;
//如果最高位是0的话i就不会++,如果是1的话,那么f[j]就不会是0,所以这步必然是进入else
}
else{
p=1;
cout<<f[j];
}
}
cout<<endl;
}
return 0;
}
▲总结:
  • 1.为什么用字符数组==>因为数字太大,long long也存储不下
  • 2.用int数组记录每一位的数字,然后模拟手算
  • 3.为什么倒置==>因为为了让末尾对齐,方便计算"个位对个位,十位对十位……"

Author: Mrli

Link: https://nymrli.top/2018/10/18/HDOJ-Problem-1002-A-B-Problem-II/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
python中关于round函数的注意事项
NextPost >
LeetCode 26. 删除排序数组中的重复项
CATALOG
  1. 1. HDOJ Problem 1002 - A + B Problem II:
    1. 1.1. Problem Description
    2. 1.2. Input
    3. 1.3. Output
    4. 1.4. Sample Input
    5. 1.5. Sample Output
    6. 1.6. 解法二:
    7. 1.7. ▲总结: