高精度减法模板(洛谷2142)
1 //luogu2142,不压位的高精度减法 2 #include <cstdio> 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 const int max_n=11000; 9 10 int a[max_n],b[max_n],c[max_n]; 11 string x,y; 12 13 //字符串转数组(倒序)的函数 14 void swi(string s,int a[]) 15 { 16 for (int i=0;i<max_n;i++) a[i]=0; 17 int n=s.size()-1; 18 for (int i=n;i>=0;i--) 19 { 20 a[0]++; 21 a[a[0]]=s[i]-'0'; 22 } 23 } 24 25 //c=a-b(a>b>0) 26 void substract(int a[],int b[],int c[]) 27 { 28 for (int i=1;i<=a[0];i++) 29 { 30 if (a[i]<b[i]) 31 { 32 a[i+1]--; 33 a[i]+=10; 34 } 35 c[i]=a[i]-b[i]; 36 } 37 c[0]=a[0]; 38 while (c[c[0]]==0 && c[0]>1) c[0]--; 39 } 40 41 //输出c 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 if (x.size()<y.size() || x.size()==y.size() && x<y) 50 { 51 printf("-"); 52 string t=x;x=y;y=t; 53 } 54 swi(x,a);swi(y,b); 55 substract(a,b,c); 56 out(c); 57 return 0; 58 }
压nn位:
1 //luogu2142,压nn位的高精度减法 2 #include <cstdio> 3 #include <iostream> 4 5 using namespace std; 6 7 const int max_n=11000,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 36 //c=a-b(a>b>0) 37 void substract(int a[],int b[],int c[]) 38 { 39 for (int i=1;i<=a[0];i++) 40 { 41 if (a[i]<b[i]) 42 { 43 a[i+1]--; 44 a[i]+=mo; 45 } 46 c[i]=a[i]-b[i]; 47 } 48 c[0]=a[0]; 49 while (c[c[0]]==0 && c[0]>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 if (x.size()<y.size() || x.size()==y.size() && x<y) 69 { 70 printf("-"); 71 string t=x;x=y;y=t; 72 } 73 swi(x,a);swi(y,b); 74 substract(a,b,c); 75 out(c); 76 return 0; 77 }
long long范围的压位(nn≤18)
1 //luogu2142,压nn位的高精度减法(long long范围的压位) 2 #include <cstdio> 3 #include <iostream> 4 5 using namespace std; 6 7 const int max_n=11000,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(a>b>0) 37 void substract(long long a[],long long b[],long long c[]) 38 { 39 for (int i=1;i<=a[0];i++) 40 { 41 if (a[i]<b[i]) 42 { 43 a[i+1]--; 44 a[i]+=mo; 45 } 46 c[i]=a[i]-b[i]; 47 } 48 c[0]=a[0]; 49 while (c[c[0]]==0 && c[0]>1) c[0]--; 50 } 51 52 //输出a 53 void out(long long a[]) 54 { 55 printf("%lld",a[a[0]]); 56 for (int i=a[0]-1;i>0;i--) 57 { 58 long long t=a[i]; 59 int k; 60 for(k=0;t>0;k++) t/=10; 61 for (int i=k+1;i<=nn;i++) printf("0"); 62 if (a[i]>0) printf("%lld",a[i]); 63 } 64 } 65 66 int main() 67 { 68 cin>>x>>y; 69 if (x.size()<y.size() || x.size()==y.size() && x<y) 70 { 71 printf("-"); 72 string t=x;x=y;y=t; 73 } 74 swi(x,a);swi(y,b); 75 substract(a,b,c); 76 out(c); 77 return 0; 78 }