【Codevs 3115】高精度练习之减法

http://codevs.cn/problem/3115/

板子题~

// <H.cpp> - Sun Oct  9 12:58:23 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
#define IN inline
#define RG register
using namespace std;
typedef long long LL;
typedef long double LB;
const int MAXN=100010;
const int MAXM=100010;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline LL gi() {
    register LL w=0,q=0;register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')q=1,ch=getchar();
    while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
    return q?-w:w;
}
const int _bod_=10;
struct BN{
    static const int N=510;int a[N];char s[N];
    int& operator [](int n){return a[n];}
    bool operator <(BN b) const{
        if(a[0]<b[0])return 1;
        if(a[0]>b[0])return 0;
        for(int i=a[0];i>=1;i--){
            if(a[i]>b[i])return 0;
            if(a[i]<b[i])return 1;
        }
        return 0;
    }
    BN operator -(BN b) const{
        BN ans=*this;int q=1;
        if(ans<b)swap(ans,b),q=-1;
        for(int i=1;i<=ans[0];i++){
            ans[i]=ans[i]-b[i];
            if(ans[i]<0){ans[i+1]--;ans[i]+=_bod_;}
        }
        while(ans[0]&&!ans[ans[0]])ans[0]--;
        for(int i=1;i<=ans[0];i++)ans[i]*=q;
        return ans;
    }
    void read(){
        scanf("%s",s);a[0]=strlen(s);
        for(int i=1;i<=a[0];i++)a[i]=s[a[0]-i]-'0';
    }
    void pri(){printf("%d",a[a[0]]);for(int i=a[0]-1;i>=1;i--)printf("%.1d",a[i]);}
}a,b;
int main()
{
    freopen("3115.in","r",stdin);
    freopen("3115.out","w",stdout);
    a.read(),b.read();
    if(a<b)printf("-"),(b-a).pri();else (a-b).pri();
    return 0;
}

 

posted @ 2016-10-17 20:01  _Mashiro  阅读(340)  评论(0编辑  收藏  举报