PAT (Basic Level) Practice (Chinese) 1017 A divided by B (20 minutes)

1017 A divided by B (20 minutes)

This problem requires calculation of A / B, where A is not more than 1000 bit positive integer, B is a positive integer. You need to output quotient Q and a remainder R, such that A = B × Q + R established.

Input formats:

Given in one row sequentially input A and B, separated by an intermediate space.

Output formats:

Sequentially outputs Q and R in a row, separated by an intermediate space.

Sample input:

123456789050987654321 7

Sample output:

17636684150141093474 3

Author: CHEN, Yue

Unit: Zhejiang University

Time limit: 100 ms

Memory Limit: 64 MB

 

Ideas:

      Tarsus simulation division.

 

 

#include <iostream>
#include <string>
using namespace std;
int main() {
    string num;
    int n = 0, sum = 0;
    cin >> num >> n;
    for (int i = 0; i < num.size(); i++) {
        if (sum == 0 &&  (num[i] -'0') < n && i != num.size() - 1) {
            if (i != 0)
                cout << '0';
            sum +=  (num[i]- '0') * 10;
            i++;
            sum +=   num[i]  -'0';
        }
        else {
            sum *= 10;
            sum += num[i]  -'0';
        }
        cout << sum / n;
        sum %= n;
    }
    cout << ' '<< sum << endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/S_999999/article/details/94557462