首页 > 其他 > 详细

大数A 除以 B(int)

时间:2020-03-06 15:32:54      阅读:49      评论:0      收藏:0      [点我收藏+]

  最近刷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;
}

 

大数A 除以 B(int)

原文:https://www.cnblogs.com/bianjunting/p/12426391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!