标签:
分析:直接上吧,建议不要使用模板,否则没啥意义了。
代码如下:
===================================================================================================================================
#include<algorithm> #include<stdio.h> #include<string.h> #include<queue> #include<math.h> using namespace std; const int MAXN = 10007; struct BigNum { int size, op;///长度, op=1表示非负数,-1表示负数 int num[MAXN];///数,下标从0开始 BigNum(){ op = size = 1; memset(num, false, sizeof(num)); } void GetText(int n) { size = log10(n)+1; for(int i=0; i<size; i++) { num[i] = n % 10; n /= 10; } } void CarryBit() {///进位 for(int i=0; i<size; i++) { if(i+1==size && num[i] >= 10) { num[i+1] = 0; size += 1; } num[i+1] += num[i] / 10; num[i] %= 10; } } void BorrowBit() {///借位 for(int i=0; i<size; i++) { if(num[i] < 0) { num[i+1] -= 1; num[i] += 10; } } while(size > 1 && !num[size-1]) size --; } bool operator < (const BigNum &a)const {///非负数比较 if(size > a.size)return false; if(size < a.size)return true; for(int i=0; i<size; i++) { if(num[i] > a.num[i])return false; if(num[i] < a.num[i])return true; } return false; } BigNum operator * (const BigNum &a)const { BigNum result; result.size = size + a.size - 1; for(int i=0; i<size; i++) for(int j=0; j<a.size; j++) { result.num[i+j] += num[i] * a.num[j]; } result.CarryBit(); return result; } friend BigNum operator - (const BigNum &_a, const BigNum &_b) { BigNum result, a=_a, b=_b; if(a < b) { result.op = -1; swap(a, b); } result.size = a.size; for(int i=0; i<a.size; i++) result.num[i] = a.num[i] - b.num[i]; result.BorrowBit(); return result; } friend BigNum operator ^(const BigNum &_a, const int &_m) { BigNum result, a=_a; result.num[0] = 1; int m = _m; while(m) { if(m & 1) result = result * a; a = a * a; m >>= 1; } return result; } void Out() { if(op == -1)printf("-"); for(int i=size-1; i>=0; i--) printf("%d", num[i]); printf("\n"); } }; int main() { int a, b; BigNum m, n; scanf("%d%d", &a, &b); m.GetText(a); n.GetText(b); m = m ^ b; n = n ^ a; BigNum ans = m - n; ans.Out(); return 0; }
标签:
原文地址:http://www.cnblogs.com/liuxin13/p/4814822.html