算法与数据结构整理-高精度

倒序存高精度整数,从个位开始对齐。输出时也倒序输出。

1.加法

 1 #include<iostream>
 2 #include<cmath>
 3 #include<string>
 4 using namespace std;
 5 string str1,str2;
 6 int a[255],b[255];
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin>>str1>>str2;
10     a[0]=str1.length();
11     b[0]=str2.length();
12     for (int i=1; i<=a[0]; i++) a[i]=str1[a[0]-i]-'0';
13     for (int i=1; i<=b[0]; i++) b[i]=str2[b[0]-i]-'0';
14     int len=max(a[0],b[0]);
15     for (int i=1; i<=len; i++) {
16         a[i]+=b[i];
17         a[i+1]+=a[i]/10;
18         a[i]%=10;
19     }
20     ++len;
21     while ((len>1) && (a[len]==0)) --len;
22     for (int i=len; i>0; i--) cout<<a[i];
23     cout<<endl;
24     return 0;
25 }

2.减法

 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<string>
 5 using namespace std;
 6 string str1,str2;
 7 int a[255],b[255];
 8 int bigger(string x,string y){
 9     int lenx=x.length(); 
10     int leny=y.length();
11     if (lenx>leny) return 1;
12     if (lenx<leny) return -1;
13     for (int i=0; i<lenx; i++){
14         if (x[i]>y[i]) return 1;
15         if (x[i]<y[i]) return -1;
16     }
17     return 0;
18 }
19 int main(){
20     ios::sync_with_stdio(false);
21     cin>>str1>>str2;
22     a[0]=str1.length();
23     b[0]=str2.length();
24     for (int i=1; i<=a[0]; i++) a[i]=str1[a[0]-i]-'0';
25     for (int i=1; i<=b[0]; i++) b[i]=str2[b[0]-i]-'0';
26     if (bigger(str1,str2)==1) {
27         for (int i=1; i<=a[0]; i++) {
28             a[i]-=b[i];
29             if (a[i]<0) --a[i+1],a[i]+=10;
30         }
31         a[0]++;
32         while (a[a[0]]==0 && a[0]) --a[0];
33         for (int i=a[0]; i>0; i--) cout<<a[i];
34             cout<<endl;
35     }
36     else if (bigger(str1,str2)==-1){
37         cout<<"-";
38         for (int i=1; i<=b[0]; i++) {
39             b[i]-=a[i];
40             if (b[i]<0) --b[i+1],b[i]+=10;
41         }
42         b[0]++;
43         while (b[b[0]]==0 && b[0]) --b[0];
44         for (int i=b[0]; i>0; i--) cout<<b[i];
45             cout<<endl;
46     }
47     else cout<<"0"<<endl;
48     return 0;
49 }

3.乘法

 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<string>
 5 using namespace std;
 6 string str1,str2;
 7 int a[255],b[255],ans[555];
 8 int main(){
 9     ios::sync_with_stdio(false);
10     cin>>str1>>str2;
11     a[0]=str1.length();
12     b[0]=str2.length();
13     for (int i=1; i<=a[0]; i++) a[i]=str1[a[0]-i]-'0';
14     for (int i=1; i<=b[0]; i++) b[i]=str2[b[0]-i]-'0';
15     for (int i=1; i<=a[0]; i++)
16         for (int j=1; j<=b[0]; j++) {
17             ans[i+j-1]+=a[i]*b[j];
18             ans[i+j]+=ans[i+j-1]/10;
19             ans[i+j-1]%=10;
20         }
21     int len=a[0]+b[0]+1;
22     while (ans[len]==0 && len>1) --len;
23     for (int i=len; i>0; i--) cout<<ans[i]; 
24     cout<<endl;
25     return 0;
26 }

4.除法(高精除以高精)

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<string>
 5 using namespace std;
 6 string str1,str2;
 7 int a[255],b[255],c[255],tmp[1007];
 8 int bigger(int x[],int y[]){
 9     if (x[0]>y[0]) return 1;
10     if (x[0]<y[0]) return -1;
11     for (int i=x[0]; i>0; i--) {
12         if (x[i]>y[i]) return 1;
13         if (x[i]<y[i]) return -1;
14     }
15     return 0;
16 }
17 void move(int x[],int y[],int j){
18     for (int i=1; i<=x[0]; i++) {
19         y[i+j-1]=x[i];
20     }
21     y[0]=x[0]+j-1;
22 }
23 void minu(int x[],int y[]){
24     for (int i=1; i<=x[0]; i++) {
25         if (x[i]<y[i]) {
26             x[i+1]--;
27             x[i]+=10;
28         }
29         x[i]-=y[i];
30     }
31     while (a[a[0]]==0) a[0]--;
32 }
33 int main(){
34     ios::sync_with_stdio(false);
35     cin>>str1>>str2;
36     a[0]=str1.length();
37     b[0]=str2.length(); 
38     for (int i=1; i<=a[0]; i++) a[i]=str1[a[0]-i]-'0';
39     for (int i=1; i<=b[0]; i++) b[i]=str2[b[0]-i]-'0';
40     if (bigger(a,b)==0) cout<<"1"<<endl;
41     else if (bigger(a,b)>0) {
42         c[0]=a[0]-b[0]+1;
43         for (int i=c[0]; i>0; i--) {
44             fill(tmp,tmp+1001,0);
45             move(b,tmp,i);
46             while (bigger(a,tmp)>=0) {
47                 ++c[i];
48                 minu(a,tmp);
49             }
50         }
51         while (c[0]>0 && c[c[0]]==0) --c[0];
52         for (int i=c[0]; i>0; i--) cout<<c[i];
53         cout<<endl;
54         for (int i=a[0]; i>0; i--) cout<<a[i];
55         cout<<endl;
56     }
57     else cout<<"0"<<endl;
58     return 0;
59 }

 

posted @ 2019-03-06 14:37  MoerBlack  阅读(140)  评论(0编辑  收藏  举报