首页 > 其他 > 详细

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

时间:2016-12-04 20:56:33      阅读:291      评论:0      收藏:0      [点我收藏+]

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;
}

 

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

原文:http://www.cnblogs.com/IUUUUUUUskyyy/p/6131182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!