zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    模块与包的导入
    递归
    day04
    装饰器2_根据认证来源判断用户和计算登录时间
    装饰器1_统计时间函数装饰欢迎登录函数
    tail -f a.txt | grep 'python'
    函数
    内置函数1
    python模块整理
    VBS恶作剧代码
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12426391.html
Copyright © 2011-2022 走看看