A/B

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

1000位的A我定义为了string类型,用A[i]-'0'来把它的每一位替换成int类型

#include <iostream>
#include<string>
using namespace std;

int main() {
    string A;
    int B, R;
    int Q[1002] = { 0 };
    int k=0, j = 0, l;

    cin >> A;
    cin >> B;
    int x = A.size();

    l = A[j] - '0';

    for (j = 0; j < x;j++) {
        l = k + (A[j]-'0');
        if (l >= B) {
            Q[j] = l / B;
        }
        k = l% B;
        k = k * 10;
        if (l < B) {
            continue;
        }
    }


    R = k / 10;

    j = 0;
    while (Q[j] == 0)
        j++;
    while(j < x) {
        cout << Q[j];
        j++;
    }

    cout << " " << R;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42082542/article/details/83039948
B
a^b
A/B
A*B