1017. A divided by B (20)

This problem requires the calculation of A/B, where A is a positive integer with no more than 1000 digits and B is a 1-digit positive integer. You need to output the quotient Q and the remainder R such that A = B * Q + R holds.

Input format:

The input gives A and B sequentially in 1 line, separated by 1 space.

Output format:

Output Q and R sequentially in 1 line, separated by 1 space.

Input sample:

123456789050987654321 7

Sample output:

17636684150141093474 3
#include<cstdio>
#include<cstring>
const int maxn = 1010;
struct bign{
    int d[maxn];
    int len;
    bign(){
        memset(d,0,sizeof(d));
        int len = 0;
    }
};

bign change(char str[]){
    bign a;
    a.len=strlen(str);
    for(int i = 0; i < a.len; i++){
        ad [i] = str [a.len- i - 1 ] - ' 0 ' ;
    }
    return a;
}

bign devide(bign a,int b,int &r){
    bign c;
    c.len = a.len;
    for(int i = a.len - 1; i >= 0; i--){
        r = r * 10 + a.d[i];
        if(r < b) c.d[i] = 0;
        else{
            c.d[i] = r/b;
            r = r % b;
        }
    }
    while(c.len - 1 >= 1 && c.d[c.len-1] == 0){
            c.len -- ;
        }
        return c;
}

void print(bign a){
    for(int i = a.len-1; i >= 0; i--){
        printf("%d",a.d[i]);
    }
}

int main(){
    char str[maxn];
    int b,r = 0;
    scanf("%s %d",str,&b);
    bign a = change(str);
    print(devide(a,b,r));
    printf(" %d",r);
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324609518&siteId=291194637