HDU 1002 - A + B Problem II - [高精度]

时间:2021-03-17 20:59:50

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

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

题意:

给出 $A,B$,求 $A+B$。

题解:

数据范围太大,需要手工模拟加法。

AC代码:

#include<stdio.h>
#include<string.h>
#define max(a,b) ((a>b)?a:b)
int main()
{
char ta[],tb[];
int a[],b[],sum[],n,t,i,lena,lenb,lenmax;
scanf("%d",&n);
for(t=;t<=n;t++)
{
scanf("%s %s",ta,tb);
lena=strlen(ta);
lenb=strlen(tb);
lenmax=max(lena,lenb);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(sum,,sizeof(sum));
for(i=;i<=lena-;i++) a[i]=ta[lena--i]-'';
for(i=;i<=lenb-;i++) b[i]=tb[lenb--i]-'';
for(i=;i<=lenmax-;i++)
{
sum[i]+=(a[i]+b[i]);
if(sum[i]>=)
{
if(i==lenmax-) lenmax++;
sum[i]-=;
sum[i+]++;
}
}
printf("Case %d:\n",t);
printf("%s + %s = ",ta,tb);
for(i=lenmax-;i>=;i--) printf("%d",sum[i]); printf("\n");
if(t<n) printf("\n");
}
}

(默默把当年刚入坑时敲得C语言代码折叠)拿出我们的高精度板子!

#include<bits/stdc++.h>
using namespace std;
const int maxn=; struct BigInt
{
int len,d[maxn]; void clean(){while(len> && !d[len-]) len--;}
void output(){for(int i=len-;i>=;i--) printf("%d",d[i]);}
string str()const
{
string s;
for(int i=;i<len;i++) s+=d[len--i]+'';
return s;
} BigInt(){memset(d,,sizeof(d));len=;}
BigInt(int num){*this=num;}
BigInt(char* num){*this=num;} bool operator<(const BigInt& oth)const
{
if(len!=oth.len) return len<oth.len;
for(int i=len-;i>=;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
return false;
}
bool operator>(const BigInt& oth)const{return oth<*this;}
bool operator<=(const BigInt& oth)const{return !(oth<*this);}
bool operator>=(const BigInt& oth)const{return !(*this<oth);}
bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);} BigInt operator=(const char* num)
{
memset(d,,sizeof(d));
len=strlen(num);
for(int i=;i<len;i++) d[i]=num[len--i]-'';
clean();
return *this;
}
BigInt operator=(int num)
{
char s[];
sprintf(s,"%d",num);
return *this=s;
}
BigInt operator+(const BigInt& oth)const
{
BigInt c;
c.len=max(len,oth.len);
for(int i=;i<=c.len;i++) c.d[i]=;
for(int i=;i<c.len;i++)
{
c.d[i]+=(i<len?d[i]:)+(i<oth.len?oth.d[i]:);
c.d[i+]+=c.d[i]/;
c.d[i]%=;
}
c.len+=(c.d[c.len]>);
c.clean();
return c;
}
BigInt operator-(const BigInt& oth)const
{
BigInt c=*this;
if(c<oth) printf("Produce negative number!\n");
int i;
for(i=;i<oth.len;i++)
{
c.d[i]-=oth.d[i];
if(c.d[i]<) c.d[i]+=, c.d[i+]--;
}
while(c.d[i]<) c.d[i++]+=, c.d[i]--;
c.clean();
return c;
}
BigInt operator*(const BigInt& oth)const
{
BigInt c;
for(int i=;i<len;i++) for(int j=;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
for(int i=;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+]+=c.d[i]/, c.d[i]%=;
c.clean();
return c;
}
BigInt operator/(const BigInt& oth)const
{
BigInt c=*this, r=;
for(int i=;i<len;i++)
{
r=r*+c.d[len--i];
int j;
for(j=;j<;j++) if(r<oth*(j+)) break;
c.d[len--i]=j;
r=r-oth*j;
}
c.clean();
return c;
}
BigInt operator%(const BigInt& oth)
{
BigInt r=;
for(int i=;i<len;i++)
{
r=r*+d[len--i];
int j;
for(j=;j<;j++) if(r<oth*(j+)) break;
r=r-oth*j;
}
return r;
}
BigInt operator+=(const BigInt& oth)
{
*this=*this+oth;
return *this;
}
BigInt operator*=(const BigInt& oth)
{
*this=*this*oth;
return *this;
}
BigInt operator-=(const BigInt& oth)
{
*this=*this-oth;
return *this;
}
BigInt operator/=(const BigInt& oth)
{
*this=*this/oth;
return *this;
}
}a,b; char A[maxn],B[maxn];
int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%s%s",A,B);
a=A, b=B;
printf("Case %d:\n",kase);
a.output();
printf(" + ");
b.output();
printf(" = ");
(a+b).output();
printf("%s",kase<T?"\n\n":"\n");
}
}