1017 A除以B (20 分)
本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。
输入格式:
输入在一行中依次给出 A 和 B,中间以 1 空格分隔。
输出格式:
在一行中依次输出 Q 和 R,中间以 1 空格分隔。
输入样例:
123456789050987654321 7
输出样例:
17636684150141093474 3
每次用第一位去除以B,如果得到的商不是0就输出,否则就*10+下一位,直到最后的数为余数
#include <iostream>
#include <string>
using namespace std;
int main(){
string a;
int b,t=0,temp=0;
cin>>a>>b;
int len=a.length();
t = (a[0]-'0')/b;
if((t!=0&&len>1) || len==1)
cout<<t;
temp = (a[0]-'0')%b;
for(int i=1;i<len;i++){
//余数是上一位的,到下一位时,需要*10
t= (temp*10+ a[i]-'0')/b;
cout<<t;
temp = (temp*10+a[i]-'0')%b;
}
cout<<" "<<temp;
return 0;
}