#P2172. 减法天才.填空题
减法天才.填空题
题目描述
在科学城里住着一位减法神童,他可以在一秒钟内算出两个数相减的结果。这两个数可不是普通的数字,它们是11位以上的“宠然大物”。为了证明自己神奇的计算能力,减法神童请全城的人都来出题考自己,只要答错一题他就自愿放弃减法神童的称号。
你想考考减法神童吗?还是先编写一个程序帮我们算出任意两个 11 位以上的数相减的精确结果吧。
输入格式
第 1 行是被减数 A
第 2 行是减数 B
数据范围
A,B 的位数大于 11,小于 200
输出格式
1行,是两个大数相加的结果。
样例
5894379463257
1245648324567
4648731138690
1245648324567
5894379463257
-4648731138690
完成程序
#include<bits/stdc++.h>
using namespace std;
struct hp{
int a[200];// 数组定义得越大,耗的内存越多
hp(){
memset(a,0,sizeof a); // 初始化为 0 很重要
}
hp(string s){
memset(a,0,sizeof a);
// 反向存储,个位放在 a[0]
for(int j=0,i=s.size()-1;i>=0;i--,j++)
a[j] = s[i] - '0';
}
void show(){
//要从高位开始输出,前面的 0 跳过不输出
bool f = true;
for(int i=199;i>=0;i--){
if(f&&a[i]==0) continue;
f = false;
printf("%d",a[i]);
}
if(f) printf("0"); // 意味着整个数组都是 0
printf("\n");
}
hp operator - (const hp &other) const{
hp tmp;// 定义临时变量,加运算的结果放在 tmp
for(int i=0;i<199;i++){
tmp.a[i] += a[i] - other.a[i]; // 特别留意,这里是 += ,不是 =
if(tmp.a[i]<0){
__填空(1)__; // 借位
tmp.a[i] += __填空(2)__;
}
}
return tmp;
}
};
int main(){
string s1,s2;
cin>>s1; // 用字符串接收高精度数字
hp a = hp(s1); //把 string 转成高精度类型的变量
cin>>s2;
hp b = hp(s2);
if(s1==s2){
cout << __填空(3)__;
return 0;
}
if(__填空(4)__||(s1.size()==s2.size()&&s1<s2)){
cout<<"-"; // 小的数减大的数,结果为负
swap(a,b); // 转变成大数 减 小数
}
hp c = __填空(5)__;
c.show(); // 调用 成员函数 输出 高精度数据
return 0;
}
填空(1):{{ input(1) }}
填空(2):{{ input(2) }}
填空(3):{{ input(3) }}
填空(4):{{ input(4) }}
填空(5):{{ input(5) }}