本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。
输入格式:
输入在1行中依次给出A和B,中间以1空格分隔。
输出格式:
在1行中依次输出Q和R,中间以1空格分隔。
输入样例:
123456789050987654321 7
输出样例:
17636684150141093474 3
这道题是小学二年级的简单除法计算,应该是超级简单的了。
// B17.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main()
{
string A;//除数
int B;//被除数
string Q;//商数
int R;//余数
int temp1;//中间过程的商
int temp2;//中间过程的余数
int m = 0;//代表被除的数
cin >> A;
cin >> B;
int length = A.size();
//如果被除数只有一位,并且小于B(也就是说商是0,余数随意)
if (length == 1 && A[0] - '0' < B)
{
cout << "0 " << A;
}
//被除数不是0
else
{
for (int i = 0; i < length; i++)
{
m = m + A[i] - '0';
//不够除
if (m < B)
{
R = m;
m = m * 10;
if (i != 0)
{
Q.append("0");
}
}
//够除
else
{
temp1 = m / B;
Q.append(to_string(temp1));//商
temp2 = m - B * temp1;
m = temp2 * 10;
R = temp2;
}
}
cout << Q << " " << R << endl;
}
system("pause");
return 0;
}