1017 A除以B(20 point(s))

版权声明:本文为博主原创文章,需表明来源,方可随意转载。 https://blog.csdn.net/qq_36589706/article/details/81938197

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

输入格式:

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

输出格式:

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

输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3
#include <iostream>
#include<cstring>
const int maxn=1100;
char str[maxn];
int main() {
	int b;
    scanf("%s%d", str, &b);
    int len=strlen(str), temp=str[0]-'0';
    int num=temp/b;
    if(len==1||(num!=0&&len!=1)){
    	printf("%d", num);
	}
	temp=temp%b;
	for(int i=1; i<len; i++){
    	if(temp<b){
    		temp=temp*10+(str[i]-'0');
		}
		num=temp/b;
		printf("%d", num);
		temp=temp%b;
	}
	printf(" %d", temp);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36589706/article/details/81938197