【PAT B-1017】A除以B

【PAT B-1017】A除以B,第1张

【PAT B-1017】A除以B C++代码
#include 
using namespace std;
using gg = long long;
//默认a为非负整数,b为正整数
pair<string, gg> DivMod(const string& a, gg b) {
    string ans;  //商
    gg mod = 0;  //余数
    for (char c : a) {
        mod = c - '0' + mod * 10;
        ans.push_back(mod / b + '0');
        mod %= b;
    }
    ans.erase(0, ans.find_first_not_of('0'));
    return {ans.empty() ? "0" : ans, mod};
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string a;
    gg b;
    cin >> a >> b;
    auto ans = DivMod(a, b);
    cout << ans.first << " " << ans.second;
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/872924.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存