B1017 A divided by B
This problem requires calculation of A / B, where A is not more than 1000 bit positive integer, B is a positive integer. You need to output quotient Q and a remainder R, such that A = B × Q + R established.
Input formats:
Given in one row sequentially input A and B, separated by an intermediate space.
Output formats:
Sequentially outputs Q and R in a row, separated by an intermediate space.
Sample input:
123456789050987654321 7
Sample output:
17636684150141093474 3
#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <math.h>
using namespace std;
struct bign{
int d[1010];
int len;
bign(){
memset(d,0,sizeof(d));
len=0;
}
};
bign change(char str[]){
bign a;
a.len=strlen(str);
for(int i=0;i<a.len;i++){
a.d[i]=str[a.len-i-1]-'0';//高位在后
}
return a;
}
bign divide(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 str1[1010];
int b,r=0;
scanf("%s%d",str1,&b);//高位在前
bign a=change(str1);
print(divide(a,b,r));
printf(" %d\n",r);
return 0;
}