高精度减法压位

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include <cstdlib>
 5 #define p 8
 6 #define carry 100000000
 7 #define maxn 10001
 8 #define maxns 2001
 9 long long ll=0;
10 char s1[maxn],s2[maxn];
11 int a[maxns],b[maxns];
12 using namespace std;
13 int change(char s[],int n[])
14 {  
15       char temp1[maxn];
16       int len=strlen(s),cur=0;
17       while(len/p)
18       {
19           strncpy(temp1,s+len-p,p);
20           n[++cur]=atoi(temp1);
21           len-=p;
22     }
23     if(len){
24         memset(temp1,0,sizeof(temp1));
25         strncpy(temp1,s,len);
26         n[++cur]=atoi(temp1);
27     }
28     return cur;
29 }
30 int add(int a[],int b[],int x1,int x2)
31 {
32     for(int i=1;i<=x2;i++)
33     {
34         a[i]-=b[i];
35         if(a[i]<0)
36         {
37             a[i+1]--;
38             a[i]+=carry;
39         }
40         //cout<<a[i]<<endl;
41     }
42     while(a[x1]==0&&x1>1)x1--;
43     return x1;
44 }
45 void swap(char w[],char y[])
46 {
47     char temp[maxn];
48     strcpy(temp,y);
49     strcpy(y,w);
50     strcpy(w,temp);
51 }
52 bool judge(char a[],char b[])
53 {   int l1,l2;
54     l1=strlen(a);
55     l2=strlen(b);
56     if(l1>l2)return false;
57     if(l2>l1)return true;
58     for(int i=0;i<l1;i++)
59         {
60       if(a[i]>b[i])return false;
61       if(a[i]<b[i])return true;
62         } 
63         return false;
64 }
65 void sin()
66 {
67     scanf("%s",s1);
68     scanf("%s",s2);
69      if(judge(s1,s2))
70      {  
71         swap(s1,s2);
72          printf("-");
73      }
74      int l1=change(s1,a);
75      int l2=change(s2,b);
76      ll=add(a,b,l1,l2);
77 }
78 void print(long long l)
79 {
80     printf("%d",a[l]);
81      for(int i=l-1;i>=1;i--)printf("%0*d",p,a[i]); 
82 
83 }
84 int main()
85 { //freopen("test1.in","r",stdin);
86   //freopen("test2out","w",stdout);
87   sin();
88   print(ll);
89   return 0;
90 }

猜你喜欢

转载自www.cnblogs.com/ltlt/p/9343072.html
今日推荐