比赛 防止浮躁的小练习v0.4 评测结果 AAAAAA
题目名称 增强的减法问题 最终得分 100
用户昵称 Fmuckss 运行时间 0.001 s
代码语言 C++ 内存使用 0.32 MiB
提交时间 2016-10-13 18:09:33
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int ten[4] = { 1, 10, 100, 1000 }; 
const int maxl = 1050;

struct BigNumber {
    int d[maxl];
    
    BigNumber() {  } 
    BigNumber(char *s, int len) {
    	memset(d, 0, sizeof(d));
        d[0] = (len - 1) / 4 + 1;
  		
  		int pos, mul;
        for(int i = len; i >= 1; i--){
            pos = (len - i) / 4 + 1;
            mul = (len - i) % 4;
            d[pos] += ten[mul] * (s[i] - '0');
        }
        
        while(d[0] > 1 and d[d[0]] == 0) --d[0];
    }
    
    void out(){
        char tmp[1000];
        int now = 1;
        
        int top;
        for (top = 3; top >= 1; top--) if (d[d[0]] >= ten[top]) break;
		
		int num = d[d[0]];
		for (int i = top; i >= 0; i--) {
			printf("%c", (char)(num / ten[i] + '0'));
			num %= ten[i];
		} 
		
		for (int i = d[0] - 1; i > 0; i--) {
			num = d[i];
			for (int j = 3; j >= 0; j--) {
			printf("%c", (char)(num / ten[j] + '0'));
			num %= ten[j];
			}
		}
    } 

    BigNumber operator - (const BigNumber &b) { 
        BigNumber c;
        c.d[0] = max(d[0], b.d[0]);
        int le = 0;
        for(int i = 1; i <= c.d[0]; i++){
            le += 10000 + d[i] - b.d[i];
            c.d[i] = le % 10000;
            le = le / 10000 - 1;
        } 
        while(c.d[0] > 1 and c.d[c.d[0]] == 0) --c.d[0];
        return c; 
    }
	
	bool operator < (const BigNumber &b) {
		if (d[0] < b.d[0]) return true;
		
		for (int i = d[0]; i >= 1; i--) {
			if (d[i] < b.d[i]) return true;
			else if (d[i] > b.d[i]) return false;
		}
		
		return false;
	}
};

char tmp_s[maxl];
int tmp_l;
BigNumber a, b, c;

#define is_num(x) (x <= '9' and x >= '0') 
inline int get_line(char *tar) {
	char tmp = getchar();
	int res = 0;
	
	while (not is_num(tmp)) tmp = getchar();
	while (is_num(tmp)) {
		tar[++res] = tmp;
		tmp  = getchar();
	}
	
	return res;
}

inline void read() {
	tmp_l = get_line(tmp_s);
	a = BigNumber(tmp_s, tmp_l);
	tmp_l = get_line(tmp_s);
	b = BigNumber(tmp_s, tmp_l);
}

inline void solve() {
	if (a < b) {
		printf("-");
		c = b - a;
		c.out(); 
	} else {
		c = a - b;
		c.out();
	}
}

int main() {
	freopen("sub.in", "r", stdin);
	freopen("sub.out", "w", stdout);
	read();
	solve();
	return 0;
}