本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string A,Q;
int B,R=0;
cin>>A>>B;
int length = A.length();
int temp = A[0]-'0';
if(temp>=B)
Q.push_back(temp/B+'0');
for(int i =1;i<length;i++)
{
R = temp%B;
temp = R*10+ A[i]-'0';
Q.push_back(temp/B+'0');
}
R = temp%B;
if(length ==1 && A[0]-'0'<B)//特别注意,当输入值A只有一位且比B小的情况
cout<<"0 "<<A[0]-'0';
else
cout<<Q<<" "<<R;
return 0;
}