PAT B --1017 A divided by B

 Think of a topic to see done before a question, large numbers input, calculates a question of individual digital bits and, AOJ's; then been thinking in that direction, the result gg ...... After reading someone else's blog came before, the original analog division of manual steps can be, hey ...... do not question some time thinking can not keep ah ah ah

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
#include<iostream>
#include<string>
using namespace std;

int main() {
    string s;
    int d,div,mod;
    cin >> s>>d;
    int len = s.length();
    div = (s[0] - '0') / d;
    mod = (s[0] - '0') % d;
    if ( div != 0 || len == 1) 
        cout << div;    
    for (int i = 1; i < len; i++) {
        div = (mod * 10 + (s[i] - '0')) / d;
        cout << div;
        mod = (mod * 10 + (s[i] - '0')) % d;
    }
    cout << ' ' << mod << endl;
    return 0;
}

 

Published 228 original articles · won praise 76 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_41895747/article/details/102944820