本題要求計算A/B,其中A是不超過1000位的正整數,B是1位正整數。你需要輸出商數Q和餘數R,使得A = B * Q + R成立。
輸入格式:
輸入在1行中依次給出A和B,中間以1空格分隔。
輸出格式:
在1行中依次輸出Q和R,中間以1空格分隔。
// 1017.cpp : 定義控制台應用程式的入口點。
//
//#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char A[1001];
int B = 0;
cin >> A >> B;
if (B > 9 || B < 1)
return 0;
int i = 0,j = 0;
int temp = 0;
char Q[1001];
int R = 0;
while (A[i] != '\0')
{
if (temp < B)
{
temp = 10 * temp + A[i++] - '0';
if (temp < B&&i!=1)
Q[j++] = '0';//右移一位仍然小于除數,當然商得上0了
}
else
{
Q[j++] = temp / B+'0';
temp = temp%B;
}
if (A[i] == '\0'&&temp!=0)//防止掃到最後一位時沒有去做除法運算
{
Q[j++] = temp / B + '0';
temp = temp%B;
}
}
R = temp;
for (int i = 0; i < j; i++)
{
cout << Q[i];
}
cout << " " << R << endl;
return 0;
}