题目:
本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。
输入格式:
输入在一行中依次给出 A 和 B,中间以 1 空格分隔。
输出格式:
在一行中依次输出 Q 和 R,中间以 1 空格分隔。
输入样例:
123456789050987654321 7
输出样例:
17636684150141093474 3
代码:
#include<iostream>
using namespace std;
int main()
{
string a;
int b;
cin>>a>>b;
int i=0;
int temp = 0;//temp表示上一位运算的余数
string ans = "";
bool frist_zero = false;//商的第一位不能为0
while(a[i]!='\0')
{
temp = temp*10 + (a[i] - '0');//类似于借位
int s = temp/b;
if(s==0&&frist_zero==false){}//商为第一位并且该位为0,不能输出
else
ans = ans+char(s+'0');
temp = temp%b;
i++;
frist_zero = true;
}
if(ans=="")
cout<<0;
cout<<ans<<" "<<temp;
}