PAT-乙级-1017. A除以B (20)

1017. A除以B (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。

输入格式:

输入在1行中依次给出A和B,中间以1空格分隔。

输出格式:

在1行中依次输出Q和R,中间以1空格分隔。

输入样例:
123456789050987654321 7
输出样例:
17636684150141093474 3
思路:将其视为字符串输入然后再模拟除法运算
 1 #include<bits/stdc++.h> 
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     string a;
 7     int b,i,first=0,temp=0;
 8     cin>>a>>b;
 9     for(i=0; i<a.length(); i++)
10     {
11         temp = temp*10+a[i]-'0';
12         if(temp>=b)
13         {
14             cout<<temp/b;
15             first = 1;
16         }
17         else if(first) cout<<0;
18         temp = temp%b;
19     }
20     if(first==0)
21     cout<<0;
22     cout<<" "<<temp<<endl;
23     return 0;
24 }


我会一直在
原文地址:https://www.cnblogs.com/zhien-aa/p/5660422.html