大数除法

洛谷:P1480
OJ: Y3441

高精度除法的主要步骤:
1、以字符串的形式读入一个高精度数和一个单精度数,分别表示被除数和除数。
2、将高精度数按位拆分,存储在数组中,同时定义一个初始值为0的变量x表示余数。
3、模拟竖式除法,从高位到低位,依次计算计算出商的每一位,并更新x。
4、从高位到低位输出数组的值为商。最后的x值为余数。

123456789/45
1/45=0, 1%45=1
12/45=0, 12%45=12
123/45=2, 123%45=33
334/45=7 , 334%45=19
195/45=4, 195%45=15
156/45=3, 156%45=21
217/45=4 217%45=37
378/45=8 378%45=18
189/45=4 189%45=9
最后结果为2743484 余数为9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**************************************************************** 
 * 代码作者: Alex Li
 * 创建时间: 2026-05-05 23:14:38
 * 最后修改: 2026-05-05 23:20:28
 * 文件描述: 大数除法
****************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {
    string a;
    long long b;
    cin >> a >> b;

    string quotient;
    long long remainder = 0;

    for (char ch : a) {
        remainder = remainder * 10 + (ch - '0');
        int digit = remainder / b;
        remainder %= b;

        if (!quotient.empty() || digit != 0) {
            quotient.push_back(digit + '0');
        }
    }

    if (quotient.empty()) {
        quotient = "0";
    }

    cout << quotient << '\n';
    cout << remainder << '\n';

    return 0;
}