PAT.大数A 除以 B(int)

  最近刷pat的时候遇到的题,才发现打比赛训练的时候都没有训练过大数除法,现在练练手写一下,其实和大数加法等思路一样,就是模拟,用字符串模拟除法,保存中间状态。

  

  直接上代码了哈。有不懂的私聊问俺。

/*
    A除以B:本题是较大的数除以一个较小的数,模拟小学除法。。。或者 java python
*/

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

vector <int> ans;

int main() {
    string A;
    int B;
    cin >> A >> B;
    int now = 0, tot = A.length() - 1, C = A[0] - '0';
    while(now <= tot) {
        if(C >= B) {
            ans.push_back(C / B);
            C = C % B;
        } else {
            now ++;
            C = C * 10 + A[now] - '0';
            ans.push_back(C / B);
            C = C % B;
        }
        if(now == tot) break;
    }
    vector <int> :: iterator i;
    for(i = ans.begin(); i != ans.end(); i ++)
        cout << *i;
    cout << ' ' << C << endl;
    return 0;
}

 

posted @ 2020-03-06 14:26  Cruel_King  阅读(192)  评论(0编辑  收藏  举报