首页 > 其他 > 详细

机试真题 大数取模运算

时间:2020-04-12 22:53:13      阅读:54      评论:0      收藏:0      [点我收藏+]

之前总结过,大数问题,取模就是取商取余数;

 

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;

string devide(int& r, string s, int n) {
    string ss = "";
    for (int i = 0; i < s.size(); i++) {
        int temp = r*10 + int(s[i] - 0);
        ss += char(0 + temp / n);
        r = temp % n;
    }
    while (ss.size() > 1 && ss[0] == 0)
        ss.erase(0, 1);
    return ss;
}

void mutipl(string &s, int n) {
    int r = 0;
    for (int i = s.size() - 1; i >= 0; i--) {
        int temp = r + int(s[i] - 0)*n;
        s[i] = char(temp % 10+0);
        r = temp / 10;
    }
    while (r != 0) {
        s = char(r % 10 + 0) + s;
        r /= 10;
    }
}


int main() {
    string s;
    while (cin >> s) {
        //cin >> s;
        mutipl(s, 225);
        cout << s << endl;
    }
    return 0;
    
}

 

机试真题 大数取模运算

原文:https://www.cnblogs.com/songlinxuan/p/12687897.html

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