高精度加法模板(洛谷1601)

洛谷P1601

不压位:

 1 //luogu1601,不压位的高精度加法
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 const int max_n=550;
 8 
 9 int a[max_n],b[max_n],c[max_n];
10 string x,y;
11 
12 //字符串转数组(倒序)的函数
13 void swi(string s,int a[])
14 {
15     for (int i=0;i<max_n;i++) a[i]=0;
16     int n=s.size()-1;
17     for (int i=n;i>=0;i--)
18     {
19         a[0]++;
20         a[a[0]]=s[i]-'0';
21     }
22 }
23 
24 //c=a+b
25 void add(int a[],int b[],int c[])
26 {
27     if (a[0]>b[0]) c[0]=a[0];
28     else c[0]=b[0];
29     for (int i=1;i<=c[0]+1;i++) c[i]=0;
30     for (int i=1;i<=c[0];i++)
31     {
32         c[i]=c[i]+a[i]+b[i];
33         if (c[i]>9)
34         {
35             c[i]-=10;c[i+1]=1;
36         }
37     }
38     if (c[c[0]+1]==1) c[0]++;
39 }
40 
41 //输出a
42 void out(int a[])
43 {
44     for (int i=a[0];i>0;i--) printf("%d",a[i]);
45 }
46 int main()
47 {
48     cin>>x>>y;
49     swi(x,a);swi(y,b);
50     add(a,b,c);
51     out(c);
52     return 0;
53 }

压nn位:

 1 //luogu1601,压nn位的高精度加法
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 const int max_n=550,nn=9,mo=1e9;//mo=10^nn
 8 
 9 int a[max_n],b[max_n],c[max_n];
10 string x,y;
11 
12 //字符串转数组(倒序)(压nn位)的函数
13 void swi(string s,int a[])
14 {
15     for (int i=0;i<=max_n;i++) a[i]=0;
16     int n=s.size()-1,i;
17     for (i=n;i>=nn-1;)
18     {
19         a[0]++;
20         int t=1;
21         for (int j=1;j<=nn;j++)
22         {
23             a[a[0]]+=(s[i]-'0')*t;
24             i--;t*=10;
25         }
26     }
27     if (i>=0)
28     {
29         a[0]++;
30         for (int j=0;j<=i;j++)
31             a[a[0]]=a[a[0]]*10+s[j]-'0';
32     }
33 }
34 
35 //c=a+b
36 void add(int a[],int b[],int c[])
37 {
38     if (a[0]>b[0]) c[0]=a[0];
39     else c[0]=b[0];
40     for (int i=1;i<=c[0]+1;i++) c[i]=0;
41     for (int i=1;i<=c[0];i++)
42     {
43         c[i]=c[i]+a[i]+b[i];
44         if (c[i]>=mo)
45         {
46             c[i]-=mo;c[i+1]=1;
47         }
48     }
49     if (c[c[0]+1]==1) c[0]++;
50 }
51 
52 //输出a
53 void out(int a[])
54 {
55     printf("%d",a[a[0]]);
56     for (int i=a[0]-1;i>0;i--)
57     {
58         int t=a[i],k;
59         for(k=0;t>0;k++) t/=10;
60         for (int i=k+1;i<=nn;i++) printf("0");
61         if (a[i]>0) printf("%d",a[i]);
62     }
63 }
64 
65 int main()
66 {
67     cin>>x>>y;
68     swi(x,a);swi(y,b);
69     add(a,b,c);
70     out(c);
71     return 0;
72 }

long long范围的压位,最多可压18位(nn≤18)

 1 //luogu1601,压nn位的高精度加法(long long范围的压位)
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 const int max_n=550,nn=12;
 8 const long long mo=1e12;//mo=10^nn
 9 
10 long long a[max_n],b[max_n],c[max_n];
11 string x,y;
12 
13 //字符串转数组(倒序)(压nn位)的函数
14 void swi(string s,long long a[])
15 {
16     for (int i=0;i<=max_n;i++) a[i]=0;
17     int n=s.size()-1,i;
18     for (i=n;i>=nn-1;)
19     {
20         a[0]++;
21         long long t=1;
22         for (int j=1;j<=nn;j++)
23         {
24             a[a[0]]+=(s[i]-'0')*t;
25             i--;t*=10;
26         }
27     }
28     if (i>=0)
29     {
30         a[0]++;
31         for (int j=0;j<=i;j++)
32             a[a[0]]=a[a[0]]*10+s[j]-'0';
33     }
34 }
35 
36 //c=a+b
37 void add(long long a[],long long b[],long long c[])
38 {
39     if (a[0]>b[0]) c[0]=a[0];
40     else c[0]=b[0];
41     for (int i=1;i<=c[0]+1;i++) c[i]=0;
42     for (int i=1;i<=c[0];i++)
43     {
44         c[i]=c[i]+a[i]+b[i];
45         if (c[i]>=mo)
46         {
47             c[i]-=mo;c[i+1]=1;
48         }
49     }
50     if (c[c[0]+1]==1) c[0]++;
51 }
52 
53 //输出a
54 void out(long long a[])
55 {
56     printf("%lld",a[a[0]]);
57     for (int i=a[0]-1;i>0;i--)
58     {
59         long long t=a[i];
60         int k;
61         for(k=0;t>0;k++) t/=10;
62         for (int i=k+1;i<=nn;i++) printf("0");
63         if (a[i]>0) printf("%lld",a[i]);
64     }
65 }
66 
67 int main()
68 {
69     cin>>x>>y;
70     swi(x,a);swi(y,b);
71     add(a,b,c);
72     out(c);
73     return 0;
74 }
原文地址:https://www.cnblogs.com/Currier/p/11288707.html