我发现一个有趣的东西:
例如2777-1888如果把每一位直接相减得到
1 -1 -1 -1 然后分别乘以他们的位权
得到1000 -100 -10 -1,加起来:
1000-100-10-1
你就得到了减法化简算式!
然后蒟蒻打了个代码
code分割线
#include <bits/stdc++.h>
using namespace std;
int a1[20],b1[20],c1[20];
bool cmp(int x,int y){return x>y;}
int main(){
long long a,b;
int i=0,j=0;
cin>>a>>b;
while(a!=0){
a1[19-i]=a%10;
a/=10,i++;
}
while(b!=0){
b1[19-j]=b%10;
b/=10,j++;
}
for(int k=0;k<max(i,j);k++){
c1[19-k]=a1[19-k]-b1[19-k];
c1[19-k]*=pow(10,k);
}
sort(c1,c1+20,cmp);
for(int k=0;k<20;k++){
if(c1[k]<0){
cout<<c1[k];
}
else if(c1[k]!=0){
if(k!=0){
cout<<'+'<<c1[k];
}
else{
cout<<c1[k];
}
}
}
return 0;
}
xuanxue输出:1000-1-10-99
答案也对但为什么计算机偷懒了呢?