题解 | 进制转换

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

string chu(string n) { //实现n/2
    int y, z, c = 0;
    for (int i = 0; i < n.size(); i++) { //实现n/2
        y = n[i] - '0' + c * 10;
        z = y % 2;
        y = y / 2;
        if (z != 0) {
            c = 1;
        } else c = 0;
        n[i] = y + '0';
    }
    if (n[0] == '0')n.erase(0, 1);
    return n;
}
int main() {
    string n;
    string t;
    cin >> n;
    while (!n.empty()) {
        int x = n[n.size() - 1] - '0';
        if (x % 2 == 0) t = "0" + t;
        else t = "1" + t;
        int y, z, c = 0;
        n = chu(n);
    }
    cout << t << endl;
    t.clear();
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务