Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
80522 sh25_zhuwy 大整数减法 C++ 通过 0 MS 272 KB 2316 2026-01-04 15:20:19

Tests(4/4):


#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N = 3000 ; struct BIG { int a[N] ; void read() { string s ; cin >> s ; a[0] = s.size() ; s = " " + s ; for(int i = 1 ; i <= a[0] ; i++ ) a[i] = s[a[0] - i + 1] - '0' ; } void print() { for(int i = 1 ; i <= a[0] ; i++ ) cout << a[a[0] - i + 1]; cout << endl ; } }; BIG operator + (const BIG &x , const BIG &y ); BIG operator - (const BIG &x , const BIG &y ); BIG operator * (const BIG &x , const int &y ); BIG operator / (const BIG &x , const int &y ); bool operator< (const BIG &x , const BIG &y ); int main() { BIG a , b ; a.read() ; b.read() ; BIG c ; c = a - b ; c.print() ; return 0 ; } BIG operator + (const BIG &x , const BIG &y ) { BIG z ; z.a[0] = max(x.a[0] , y.a[0] ) ; int u = 0 ; for(int i = 1 ; i <= z.a[0] ; i++ ) { int t = u ; if(x.a[0] >= i) t += x.a[i] ; if(y.a[0] >= i) t += y.a[i] ; u = t / 10 ; z.a[i] = t % 10 ; } if(u > 0) z.a[0]++ , z.a[z.a[0]] = u ; return z ; } BIG operator - (const BIG &x , const BIG &y ) { BIG z ; z.a[0] = max(x.a[0] , y.a[0]) ; for(int i = 1 ; i <= z.a[0] ; i++ ) z.a[i] = x.a[i] - y.a[i] ; for(int i = 1 ; i <= z.a[0] ; i++ ) if(z.a[i] < 0) { z.a[i] += 10 ; z.a[i + 1]-- ; } while(z.a[z.a[0]] == 0 && z.a[0] > 1) z.a[0]-- ; return z ; } bool operator < (const BIG &x , const BIG &y ) { if(x.a[0] != y.a[0]) return x.a[0] < y.a[0] ; for(int i = x.a[0] ; i >= 1 ; i-- ) if(x.a[i] != y.a[i]) return x.a[i] < y.a[i] ; return false ; } BIG operator * (const BIG &x , const int &y ) { BIG z ; z.a[0] = x.a[0] ; for(int i = 1 ; i <= z.a[0] ; i++ ) z.a[i] = x.a[i] * y ; for(int i = 1 ; i <= z.a[0] ; i++ ) { z.a[i + 1] += z.a[i] / 10 ; z.a[i] %= 10 ; } while(z.a[z.a[0] + 1] > 0) { z.a[0]++ ; z.a[z.a[0] + 1] += z.a[z.a[0]] / 10 ; z.a[z.a[0]] %= 10 ; } return z ; } BIG operator / (const BIG &x , const int &y ) { BIG z ; z.a[0] = x.a[0] ; int r = 0 ; for(int i = z.a[0] ; i >= 1 ; i-- ) { z.a[i] = (r * 10 + x.a[i]) / y ; r = (r * 10 + x.a[i]) % y ; } while(z.a[z.a[0]] == 0 && z.a[0] > 1) z.a[0]-- ; return z ; }


测评信息: