您的位置:首页 > 运维架构

Ac日记——大整数减法 openjudge 1.6 11

2016-12-04 17:00 507 查看

11:大整数减法

总时间限制: 1000ms 内存限制: 65536kB描述
求两个大的正整数相减的差。

输入共2行,第1行是被减数a,第2行是减数b(a > b)。每个大整数不超过200位,不会有多余的前导零。输出一行,即所求的差。样例输入
9999999999999999999999999999999999999
9999999999999

样例输出
9999999999999999999999990000000000000


思路:

  模拟;

来,上代码:

#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

char s1[221],s2[221];

void exchange()
{
int l=0,r=strlen(s1)-1;
while(r>l)
{
if(s1[l]>='0') s1[l]-='0';
if(s1[r]>='0') s1[r]-='0';
swap(s1[l],s1[r]);
l++,r--;
}
if(l==r) s1[l]-='0';
l=0,r=strlen(s2)-1;
while(r>l)
{
if(s2[l]>='0') s2[l]-='0';
if(s2[r]>='0') s2[r]-='0';
swap(s2[l],s2[r]);
l++,r--;
}
if(l==r) s2[l]-='0';
}

int main()
{
cin>>s1;
cin>>s2;
exchange();
//cout<<s1<<endl<<s2<<endl;
for(int i=0;i<=219;i++)
{
if(s1[i]-s2[i]<0) s1[i+1]--,s1[i]+=10;
s1[i]-=s2[i];
}
bool if_zero=true;
for(int i=219;i>=0;i--)
{
if(s1[i]==0) continue;
if_zero=false;
for(int j=i;j>=0;j--) putchar(s1[j]+'0');
break;
}
if(if_zero) putchar('0');
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: