大整數減法
阿新 • • 發佈:2018-10-01
pre nbsp algo turn code -i ring class div
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 2000; int a[maxn]; int b[maxn]; int c[maxn]; char s[maxn]; char t[maxn]; char n[maxn]; int main(void) { cin>>s>>t;int lens = strlen(s); int lent = strlen(t); if(lens<lent||lens==lent&&strcmp(s,t)<0) { strcpy(n,s); strcpy(s,t); strcpy(t,n); cout<<"-"; } lens = strlen(s); lent = strlen(t); for(int i = 0; i < lens; i++) a[lens-i] = s[i]-‘0‘; for(int i = 0; i < lent; i++) b[lent-i] = t[i]-‘0‘; int lenc = 1; while(lenc<=lens||lenc<=lent) { if(a[lenc]<b[lenc]) { a[lenc] += 10; a[lenc+1]--; } c[lenc] = a[lenc]-b[lenc]; lenc++; } while(c[lenc]==0) lenc--; for(int i = lenc; i >= 1; i--) cout<<c[i]; cout<<endl; return 0; }
大整數減法