机试真题 10进制转换为8进制

老生常谈的问题,留商取余;

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

int n;

string charge(int n) {
    string s="";
    while (n != 0) {
        int r = n % 8;
        s = char('0' + r) + s;
        n = n / 8;
    }
    return s;
}

int main() {
    cin >> n;
    cout << charge(n) << endl;
}

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12681297.html