站外题“任意进制转换”求助
  • 板块学术版
  • 楼主BunDragon126
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/4/19 22:31
  • 上次更新2024/4/20 09:54:50
查看原帖
站外题“任意进制转换”求助
927203
BunDragon126楼主2024/4/19 22:31

代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    long long n;
    cin >> n;
    
    if(n == 0)
    {
    	cout << 0 << endl << 0 << endl << 0 ;
    }
    
    if(n == 1)
    {
    	cout << 1 << endl << 1 << endl << 1 ;
    }

    string binary = "";
    long long temp = n;
    while (temp > 0) {
        binary = to_string(temp % 2) + binary;
        temp /= 2;
    }
    if(n < 0)
    {
    	cout << 1 << binary << endl;
    }
    else
    {
    	cout << binary << endl;
    }
    

    string octal = "";
    temp = n;
    while (temp > 0) {
        octal = to_string(temp % 8) + octal;
        temp /= 8;
    }
    cout << octal << endl;

    string hex = "";
    temp = n;
    while (temp > 0) {
        long long remainder = temp % 16;
        if (remainder < 10) {
            hex = to_string(remainder) + hex;
        } else {
            hex = static_cast<char>('A' + (remainder - 10)) + hex;
        }
        temp /= 16;
    }
    cout << hex << endl;

    return 0;
}
2024/4/19 22:31
加载中...