PAT-B 1017 A除以B
https://pintia.cn/problem-sets/994805260223102976/problems/994805305181847552
题目
本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。
输入
输入在一行中依次给出 A 和 B,中间以 1 空格分隔。
输出
在一行中依次输出 Q 和 R,中间以 1 空格分隔。
样例输入
123456789050987654321 7
样例输出
17636684150141093474 3
分析
使用字符串接收大数,注释很详细,具体看程序。
C++程序
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
int b;
cin>>a>>b;
int temp=0;
bool flag=false;//是否输出过非零位
for(int i=0;i<a.length();i++)
{
temp=temp*10+a[i]-'0';
int x=temp/b;
temp%=b;
//如果首位为0就跳过(防止出现前导零)
if(!flag&&x==0) continue;
cout<<x;//输出商的这一位
flag=true;//已经输出过非零位了,置为true,以后出现0就可以输出了
}
if(!flag) cout<<"0";//商为0的情况
cout<<" "<<temp<<endl;//输出余数
return 0;
}