GitHub

大整数除法(1570)

这道题是有难度的,注意超时问题,回溯法

题目描述

求两个不超过100位的正整数相除的商。

输入描述

第1 行是测试数据的组数n,每组测试数据占2 行,第1 行是被除数,第2 行是除数,每行数据不超过100位。

输出描述

n 行,每组测试数据有一行输出是相应的整数商

样例输入

2

10000000000000000000000000000000000000000

10000000000

5409656775097850895687056798068970934546546575676768678435435345

134563

样例输出

1000000000000000000000000000000

40201665949019053496778882739452679670834825142697239794263

 

 1 #include<iostream>
 2 using namespace std;
 3 int main(){
 4     int n,i;
 5     cin>>n;
 6     for(i=0;i<n;i++){
 7         long long a,b,p,q;
 8         cin>>a;
 9         cin>>b;
10         p=a%b;
11         q=(a-p)/b;
12         cout<<q<<endl;
13     }
14     return 0;
15 }

 

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 string minus(string a,string b){
 5     int i,j,length,p;
 6     int per=0,k=0;
 7     string zero="0";
 8     length=a.size()-b.size();
 9     j=a.size()-1;
10     int min[j];
11     string zeros(length,'0');
12     b=zeros+b;
13     for(i=j;i>=0;i--){
14         p=(a[i]-'0')-(b[i]-'0')-per;
15         if(p<0){
16             p=p+10;
17             min[j--]=p;
18             per=1;
19         }
20         else{
21             min[j--]=p;
22             per=0;
23         }
24     }
25     while(min[k]==0){
26         k++;
27         if(k==a.size()){
28             break;
29         }
30     }
31     if(k==a.size()){
32         return zero;
33     }
34     else{    
35         for(i=k;i<a.size();i++){
36             min[i]=min[i]+'0';
37         }
38         return min;
39     }
40 }
41 int main(){
42     string times="0";
43     string p,q,ss;
44     int n,i;
45     for(i=0;i<n;i++){
46         cin>>p;
47         cin>>q;
48     }
49     while(ss-"0">0){
50         ss=minus(p,q)
51         p=ss;
52         time=time+1;
53     }
54     cout<<time<<endl;
55 }
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 using namespace std;
 5 char l1[300];
 6 char l2[300];
 7 int a1[300];
 8 int a2[300];
 9 int as[300];
10 int sub(int *p1,int *p2,int nl1,int nl2){
11     int i;
12     if(nl1<nl2){
13         return -1;
14     }
15     if(nl1==nl2){
16         for(i=nl1-1;i>=0;i--){
17             if(p1[i]>p2[i]){
18                 break;
19             }
20             else if(p1[i]<p2[i]){
21                 return -1;
22             }
23         }
24     }
25     for(i=0;i<nl1;i++){
26         p1[i]-=p2[i];
27         if(p1[i]<0){
28             p1[i]+=10;
29             p1[i+1]--;
30         }
31     }
32     for(i=nl1-1;i>=0;i--){
33         if(p1[i]){
34             return i+1;
35         }
36         return 0;
37     }
38 }
39 int main(){
40     int t,n;
41     cin>>t;
42     for(n=0;n<t;n++){
43         int i,j,n1,n2,nn;
44         cin>>l1;
45         cin>>l2;
46         n1=strlen(l1);
47         n2=strlen(l2);
48         nn=n1-n2;
49         memset(a1,0,sizeof(a1));
50         memset(a2,0,sizeof(a2));
51         memset(as,0,sizeof(as));
52         for(j=0,i=n1-1;i>=0;i--){
53             a1[j++]=l1[i]-'0';
54         }
55         for(j=0,i=n2-1;i>=0;i--){
56             a2[j++]=l2[i]-'0';
57         }
58         if(n1<n2){
59             cout<<"0"<<endl;
60             continue;
61         }
62         if(nn>0){
63             for(i=n1-1;i>=nn;i--){
64                 a2[i]=a2[i-nn];
65             }
66             for(;i>=0;i--){
67                 a2[i]=0;
68             }
69             n2=n1;
70         }
71         for(j=0;j<=nn;j++){
72             int nt;
73             while((nt=sub(a1,a2+j,n1,n2-j))>=0)
74             {
75                 n1=nt;
76                 as[nn-j]++;
77             }
78         }
79         for(i=200;(i>=0)&&(as[i]==0);i--);
80         if(i>=0){
81             for(;i>=0;i--){
82                 cout<<as[i];
83             }
84         }
85         else{
86             cout<<"0";
87         }
88         cout<<endl;
89     }
90     return 0;
91 }
View Code
 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 int dd[101],ds[101],qu[101];
 5 char str1[101],str2[101];
 6 int sb(int* dd,int* ds,int len1,int len2)
 7 {
 8     int i;
 9     if (len1<len2)  return -1;
10     else if (len1==len2)
11         for (i=len1-1;i>=0;i--)
12         {
13             if (dd[i]<ds[i])
14                     return -1;
15             else if (dd[i]>ds[i])
16                 break;
17         }
18     for (i=0;i<len1;i++)
19     {
20         dd[i]-=ds[i];
21         if (dd[i]<0)
22         {
23             dd[i]+=10;
24             dd[i+1]--;
25         }
26     }
27     for (i=len1-1;i>=0;i--)
28         if (dd[i])  break;
29     return i+1;
30 }
31 
32 int main()
33 {
34     int ci,ni;
35     cin>>ci;
36     for(ni=0;ni<ci;ni++){
37         int i,j,k;
38         gets(str1);
39         gets(str2);
40         int len1=strlen(str1),len2=strlen(str2);
41         for (k=0,i=len1-1;i>=0;i--)
42             dd[k++]=str1[i]-'0';
43         for (k=0,i=len2-1;i>=0;i--)
44             ds[k++]=str2[i]-'0';
45         len1=sb(dd,ds,len1,len2);
46         if (len1==-1)
47         {
48             printf("0\n");
49             return 0;
50         }
51         else if (len1==0)
52         {
53             printf("1\n");
54             return 0;
55         }
56         qu[0]=1;
57         int times=len1-len2;
58         for (i=len1-1;i>=0;i--)
59         {
60             if (i>=times)
61                 ds[i]=ds[i-times];
62             else
63                 ds[i]=0;
64         }
65         len2=len1;
66         for (j=0;j<=times;j++)
67         {
68             int tmp;
69             while ((tmp=sb(dd,ds+j,len1,len2-j))>=0)
70             {
71                 len1=tmp;
72                 qu[times-j]++;
73             }
74         }
75         for (i=0;i<101;i++)
76         {
77             if (qu[i]>9)
78             {
79                 qu[i+1]+=qu[i]/10; 
80                 qu[i]%=10;
81             }       
82         }
83         for (i=100;qu[i]==0&&i>=0;i--);
84         while (i>=0)
85         cout<<qu[i--];
86         cout<<endl;
87     }    
88     return 0;
89 }
  1 #include <iostream>
  2 #include <cstring>
  3 #define MAX 100
  4 using namespace std;
  5 int an1[MAX + 10];
  6 int an2[MAX + 10];
  7 int aResult[MAX + 10];
  8 char szLine1[MAX + 10];
  9 char szLine2[MAX + 10];
 10 int Substract(int *p1,int *p2,int nLen1,int nLen2){
 11     int i;
 12     if(nLen1 < nLen2){
 13         return -1;
 14     }
 15     bool bLarger = false;
 16     if(nLen1 == nLen2){
 17         for(i = nLen1 - 1;i >= 0;i--){
 18             if(p1[i] > p2[i]){
 19                 bLarger = true;
 20             }
 21             if(p1[i] < p2[i]){
 22                 if(!bLarger)
 23                     return -1;
 24             }
 25         }
 26     }
 27     for(i=0;i<nLen1;i++){
 28         p1[i] -= p2[i];
 29         if(p1[i] < 0){
 30             p1[i] += 10;
 31             p1[i+1]--;
 32         }
 33     }
 34     for(i=nLen1-1;i >= 0;i--){
 35         if(p1[i]){
 36             return i+1;
 37         }
 38     }
 39     return 0;
 40 }
 41 int main()
 42 {
 43     int n;
 44     char szBlank[20];
 45     cin >> n;
 46      while(n--){
 47          cin >> szLine1;
 48          cin >> szLine2;
 49          int i,j;
 50          int nLen1 = strlen(szLine1);
 51          int nLen2 = strlen(szLine2);
 52          memset(an1,0,sizeof(an1));
 53          memset(an2,0,sizeof(an2));
 54          memset(aResult,0,sizeof(aResult));
 55          j = 0;
 56          for(i=nLen1-1;i>=0;i--){
 57              an1[j++]=szLine1[i] - '0';
 58          }
 59          j = 0;
 60          for(i=nLen2-1;i>=0;i--){
 61              an2[j++]=szLine2[i] - '0';
 62          }
 63          if(nLen1 < nLen2) {
 64              cout<<"0"<<endl;
 65              continue;
 66          }
 67          nLen1 = Substract(an1,an2,nLen1,nLen2);
 68          if(nLen1 == -1){
 69              cout<<"0"<<endl;
 70              continue;
 71          }else if(nLen1 == 0){
 72              cout<<"1"<<endl;
 73              continue;
 74          }
 75          aResult[0]++;
 76          int nTimes = nLen1 - nLen2;
 77         if(nTimes < 0){
 78              goto Result;
 79          }else if(nTimes > 0){
 80              for(i=nLen1-1;i>=0;i--){
 81                  if(i >= nTimes){
 82                      an2[i]=an2[i-nTimes];
 83                  }else{
 84                      an2[i]=0;
 85                  }
 86              }
 87          }
 88          nLen2 = nLen1;
 89          for(j=0;j<=nTimes;j++){
 90              int nTemp;
 91              while((nTemp=Substract(an1,an2+j,nLen1,nLen2-j)) >= 0){
 92                  nLen1 = nTemp;
 93                  aResult[nTimes-j]++;
 94              }
 95          }
 96 Result:
 97         for(i=0;i<MAX;i++){
 98             if(aResult[i]>=10){
 99                 aResult[i+1] += aResult[i] / 10;
100                 aResult[i] %= 10;
101             }
102         }
103         bool bStartOutput = false;
104         for(i=MAX;i>=0;i--){
105             if(bStartOutput){
106                 cout<<aResult[i];
107             }else if(aResult[i]){
108                 cout<<aResult[i];
109                 bStartOutput = true;
110             }
111         }
112         if(!bStartOutput){
113             cout<<"0";
114         }
115         cout<<endl;
116      }
117     return 0;
118 }
zz
  1 #include <iostream>
  2 #include <cstring>
  3 #define MAX_LEN 200
  4 using namespace std;
  5 char szLine1[MAX_LEN + 10];
  6 char szLine2[MAX_LEN + 10];
  7 int an1[MAX_LEN + 10];//被除数, an1[0]对应于个位
  8 int an2[MAX_LEN + 10];//除数, an2[0]对应于个位
  9 int aResult[MAX_LEN + 10]; //存放商, aResult[0]对应于个位
 10 /*Substract函数:
 11 长度为 nLen1 的大整数 p1 减去长度为 nLen2 的大整数 p2
 12 减的结果放在 p1 里,返回值代表结果的长度
 13 如不够减返回-1,正好减完返回 0
 14 p1[0]、 p2[0] 是个位 */
 15 int Substract(int *p1,int *p2,int nLen1,int nLen2){
 16     int i;
 17     if(nLen1 < nLen2){
 18         return -1;
 19     }
 20     //下面判断 p1 是否比 p2 大,如果不是,返回-1
 21     bool bLarger = false;
 22     if(nLen1 == nLen2){
 23         for(i = nLen1 - 1;i >= 0;i--){
 24             if(p1[i] > p2[i]){
 25                 bLarger = true;
 26             }
 27             if(p1[i] < p2[i]){
 28                 if(!bLarger)
 29                     return -1;
 30             }
 31         }
 32     }
 33     for(i=0;i<nLen1;i++){//做减法
 34         p1[i] -= p2[i];//要求调用本函数时给的参数能确保当 i>=nLen2 时, p2[i] = 0
 35         if(p1[i] < 0){
 36             p1[i] += 10;
 37             p1[i+1]--;
 38         }
 39     }
 40     for(i=nLen1-1;i >= 0;i--){
 41         if(p1[i]){
 42             return i+1;
 43         }
 44     }
 45     return 0;
 46 }
 47 int main()
 48 {
 49     int n;
 50     char szBlank[20];
 51     cin >> n;
 52      while(n--){
 53          cin >> szLine1;
 54          cin >> szLine2;
 55          int i,j;
 56          int nLen1 = strlen(szLine1);
 57          int nLen2 = strlen(szLine2);
 58          memset(an1,0,sizeof(an1));
 59          memset(an2,0,sizeof(an2));
 60          memset(aResult,0,sizeof(aResult));
 61          j = 0;
 62          for(i=nLen1-1;i>=0;i--){
 63              an1[j++]=szLine1[i] - '0';
 64          }
 65          j = 0;
 66          for(i=nLen2-1;i>=0;i--){
 67              an2[j++]=szLine2[i] - '0';
 68          }
 69          if(nLen1 < nLen2) {
 70              cout<<"0"<<endl;
 71              continue;
 72          }
 73          nLen1 = Substract(an1,an2,nLen1,nLen2);
 74          if(nLen1 == -1){
 75              cout<<"0"<<endl;
 76              continue;
 77          }else if(nLen1 == 0){
 78              cout<<"1"<<endl;
 79              continue;
 80          }
 81          aResult[0]++;//减掉一次了,商加 1
 82          int nTimes = nLen1 - nLen2;
 83         if(nTimes < 0){//减一次后就不能再减了
 84              goto OutputResult;
 85          }else if(nTimes > 0){
 86              //将 an2 乘以 10 的某次幂,使得结果长度和 an1 相同
 87              for(i=nLen1-1;i>=0;i--){
 88                  if(i >= nTimes){
 89                      an2[i]=an2[i-nTimes];
 90                  }else{
 91                      an2[i]=0;
 92                  }
 93              }
 94          }
 95          nLen2 = nLen1;
 96          for(j=0;j<=nTimes;j++){
 97              int nTemp;
 98              //一直减到不够减为止
 99              //先减去若干个 an2×(10 的 nTimes 次方),
100              //不够减了,再减去若干个 an2×(10 的 nTimes-1 次方), ......
101              while((nTemp=Substract(an1,an2+j,nLen1,nLen2-j)) >= 0){
102                  nLen1 = nTemp;
103                  aResult[nTimes-j]++;//每成功减一次,则将商的相应位加 1
104              }
105          }
106 OutputResult:
107         //下面的循环统一处理进位问题
108         for(i=0;i<MAX_LEN;i++){
109             if(aResult[i]>=10){
110                 aResult[i+1] += aResult[i] / 10;
111                 aResult[i] %= 10;
112             }
113         }
114         //下面输出结果
115         bool bStartOutput = false;
116         for(i=MAX_LEN;i>=0;i--){
117             if(bStartOutput){
118                 cout<<aResult[i];
119             }else if(aResult[i]){
120                 cout<<aResult[i];
121                 bStartOutput = true;
122             }
123         }
124         if(!bStartOutput){
125             cout<<"0";
126         }
127         cout<<endl;
128      }
129     return 0;
130 }
摘自

 

posted @ 2019-06-22 20:33  繁华似锦觅安宁  阅读(1436)  评论(0编辑  收藏  举报