本题要求计算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 <iostream>
#include <string>
using namespace std;
int main()
{
string a;//用字符串来储存1000位的数字
int b,highist=0,temp = 0;
cin >> a >> b;
for (int i = 0 ;i < a.length() ; i++)
{
temp = temp * 10 + a[i] - '0';//把字符串拆成数字,按照从高到低位的顺序
if(temp >= b)
{
highist = 1;//这是个标志位,用于某位是否输出0
cout << temp/b; //先求模
}
else if (highist) cout << 0;
temp = temp % b;//temp是余数,把余数在下一回合*10 作为新的被除数
}
if (0 == highist)cout << 0;//如果a<b 输出一个0就够了
cout << " " << temp << endl;
// cout << "Hello world!" << endl;
return 0;
}
本文介绍了一种处理大数除法的算法实现方法,针对A/B运算中的A为不超过1000位的大整数,B为1位数的情况。通过逐位计算商数Q和余数R,确保A=B*Q+R成立。
1332

被折叠的 条评论
为什么被折叠?



