B1017. A除以B (20)

本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。

输入格式:

输入在1行中依次给出A和B,中间以1空格分隔。

输出格式:

在1行中依次输出Q和R,中间以1空格分隔。

输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <algorithm>
 8 using namespace std;
 9 
10 struct bign
11 {
12     int d[1010];
13     int len;
14     bign(){
15         memset(d,0,sizeof(d));
16         len=0;
17     }
18 };
19 
20 bign change(char str[]){
21     bign a;
22     a.len=strlen(str);
23     for(int i=0;i<a.len;i++)
24     {
25         a.d[i]=str[a.len-i-1]-'0';
26     }
27     return a;
28 }
29 
30 bign divide(bign a,int b,int & r)
31 {
32     bign c;
33     c.len=a.len;
34     for(int i=a.len-1;i>=0;i--)
35     {
36          r=10*r+a.d[i];
37          if(r<b)c.d[i]=0;
38          else
39          {
40           c.d[i]=r/b;
41           r=r%b;    
42          }    
43     }
44     while(c.len-1>0&&c.d[c.len-1]==0)
45     {
46         c.len--;
47     }
48     return c;
49 }
50 
51 void print(bign a)
52 {
53   for(int i=a.len-1;i>=0;i--)
54   {
55       printf("%d",a.d[i]);
56   }    
57 }
58 
59 int main(){
60      char a[1010];
61      int b;
62      scanf("%s %d",a,&b);
63      bign m=change(a);
64      int r=0;
65      print(divide(m,b,r));
66      printf(" %d",r);
67     return 0;
68 }
原文地址:https://www.cnblogs.com/ligen/p/4304014.html