• 高精度模板


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define N 10100
    using namespace std;
    const int base = 10;
    struct Num {
    	int num[N], len;
    	Num(){ memset(num, 0, sizeof(num)); len = 0; }
    	inline void clear() { memset(num , 0, sizeof(num)); len = 0; }
    	Num(int x) {
    		clear();
    		while (x) num[len++] = x % base, x /= base;
    	}
    	inline void Print(char c = '
    ') {
    		if (!len) putchar('0');
    		for (int i = len - 1; ~i; --i) printf("%d", num[i]);
    		putchar(c);
    	}
    };
    inline bool operator <(const Num &a, const Num &b) {
    	if (a.len != b.len) return a.len < b.len;
    	for (int i = a.len - 1; ~i; --i) if (a.num[i] != b.num[i])
    		return a.num[i] < b.num[i];
    	return false;
    }
    inline Num operator +(const Num &a, const int b) {
    	Num c;
    	int len = a.len, r = b;
    	for (int i = 0; i < len; ++i) {
    		r += a.num[i];
    		c.num[i] = r % base;
    		r /= base;
    	}
    	while (r) c.num[len++] = r % base, r /= base;
    	c.len = len;
    	return c;
    }
    inline Num operator +(const Num &a, const Num &b) {
    	Num c;
    	int len = max(a.len, b.len);
    	for (int i = 0; i < len; ++i) {
    		c.num[i] = c.num[i] + a.num[i] + b.num[i];
    		if (c.num[i] >= base) ++c.num[i + 1], c.num[i] -= base;
    	}
    	if (c.num[len]) ++len;
    	c.len = len;
    	return c;
    }
    inline Num operator -(const Num &a, const Num &b) {//a > b, return a - b
    	Num c;
    	int len = a.len;
    	for (int i = 0; i < len; ++i) {
    		c.num[i] = (c.num[i] + a.num[i] - b.num[i]);
    		if (c.num[i] < 0) --c.num[i + 1], c.num[i] += base;
    	}
    	while (len && !c.num[len - 1]) --len;
    	c.len = len;
    	return c;
    }
    inline Num operator *(const Num &a, const int b) {
    	Num c = a;
    	if (!b) return Num();
    	int r = 0, len = a.len;
    	for (int i = 0; i < len; ++i) {
    		r = (r + a.num[i] * b);
    		c.num[i] = r % base;
    		r /= base;
    	}
    	while (r) c.num[len++] = r % base, r /= base;
    	c.len = len;
    	return c;
    }
    inline Num operator /(const Num &a, const int b) {
    	Num c;
    	int r = 0, len = a.len;
    	for (int i = len - 1; ~i; --i) {
    		r = r * base + a.num[i];
    		c.num[i] = r / b;
    		r %= b;
    	}
    	while (len && !c.num[len]) --len;
    	c.len = len;
    	return c;
    }
    inline Num operator <<(const Num &a, const int b) {
    	Num c;
    	int len = a.len;
    	for (int i = len - 1; ~i; --i) c.num[i + b] = a.num[i];
    	c.len = len + b;
    	return c;
    }
    int main() {
    	Num jzp;
    	jzp = jzp * 21323 + 1213;
    	jzp = jzp * 123 + 4123;
    	jzp = jzp << 3;
    	jzp.Print();
    	Num zzz;
    	zzz = zzz + 2145123;
    	zzz = zzz * 4213;
    	zzz = zzz << 5;
    	zzz.Print();
    	(zzz - jzp).Print();
    	return 0;
    }
    
  • 相关阅读:
    Python连接MySQL乱码(中文变问号)
    mysql学习04 pymysql 学习
    mysql学习03
    多态与多态性
    重用父类功能的两种方式
    菱形继承问题
    组合
    继承的应用和派生的概念引出
    类的继承
    类与类型
  • 原文地址:https://www.cnblogs.com/JiaZP/p/14883393.html
Copyright © 2020-2023  润新知