【PAT甲级 进制转换】1027 Colors in Mars (20 分)

给定三个十进制数字,转13进制

#include<bits/stdc++.h>
using namespace std;

map<int, char> M = {
    
    
    {
    
    0, '0'}, {
    
    1, '1'}, {
    
    2, '2'}, {
    
    3, '3'}, {
    
    4, '4'}, {
    
    5, '5'},
    {
    
    6, '6'}, {
    
    7, '7'}, {
    
    8, '8'}, {
    
    9, '9'}, {
    
    10, 'A'}, {
    
    11, 'B'}, {
    
    12, 'C'}
};

string convert(int n, int radix = 13){
    
     // 10转13
    string rst;
    do{
    
    
        rst.insert(rst.begin() ,M[n % radix] );
        n /= radix;
    } while(n != 0);
    return rst;
}

int main() {
    
    
    cout << '#';
    for(int i=0;i<3;++i){
    
    
        int num;
        cin>>num;
        string num_13 = convert(num);
        if(num_13.size() == 1)
            cout << 0;
        cout << num_13 ;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/MYMarcoreus/article/details/114549000
今日推荐