1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include<iostream> #include<string> #include<cstring> using namespace std;
int newcopy(int a[],int b[],int n); int cmp(int a[],int b[]); int jianfa(int a[],int b[]); int print(int a[]); int chugao(int a[],int b[],int c[]);
int main() { string s1,s2; int a[110],b[110],c[110]; while(cin>>s1>>s2) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); a[0]=s1.length(); b[0]=s2.length(); for(int i=1;i<=a[0];i++) a[i]=s1[a[0]-i]-'0'; for(int i=1;i<=b[0];i++) b[i]=s2[b[0]-i]-'0'; chugao(a,b,c); print(c); print(a); } return 0; }
int chugao(int a[],int b[],int c[]) { c[0]=a[0]-b[0]+1; for(int i=c[0];i>0;i--) { int temp[110]; memset(temp,0,sizeof(temp)); newcopy(b,temp,i); while(cmp(a,temp)>=0) { c[i]++; jianfa(a,temp); } } while(c[0]>0&&c[c[0]]==0) c[0]--; return 0; }
int print(int a[]) { if(a[0]==0) cout<<0<<endl; else { for(int i=a[0];i>0;i--) cout<<a[i]; cout<<endl; } return 0; }
int jianfa(int a[],int b[]) { if(cmp(a,b)==0) { a[0]=0; return 0; } if(cmp(a,b)==1) { for(int i=1;i<=a[0];i++) { if(a[i]<b[i]) { a[i+1]--; a[i]+=10; } a[i]-=b[i]; } while(a[0]>0&&a[a[0]]==0) a[0]--; } return 0; }
int newcopy(int a[],int b[],int n) { for(int i=1;i<=a[0];i++) b[i+n-1]=a[i]; b[0]=a[0]+n-1; return 0; }
int cmp(int a[],int b[]) { if(a[0]>b[0]) return 1; if(a[0]<b[0]) return -1; for(int i=a[0];i>0;i--) { if(a[i]>b[i]) return 1; if(a[i]<b[i]) return -1; } return 0; }
|