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

此题为一道纯模拟题,模拟除法,感觉特别经典。。。。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;

const int maxn = 1100;
char s[maxn];

int main()
{
	int n;
	scanf("%s%d", s, &n);
	int t = strlen(s);
	if(t == 1){
		int now = (s[0] - '0') / n;
		int temp = (s[0] - '0') % n;
		printf("%d %d\n", now, temp);
	}
	else{
		int now, temp = s[0] - '0';
		for(int i = 1; i < t; i++){
			now = temp*10 + (s[i] - '0');
			printf("%d", now / n);
			temp = now % n;
		}
		printf(" %d\n", temp);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41818544/article/details/83689791