PAT_A 1027 Colors in Mars

PAT_A 1027 Colors in Mars

分析

本题只需将输入的3个10进制数转换为13进制数,再按指定格式输出即可,如果打表可能会快一点 ( ゚∀。)

题目的描述

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input Specification:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:

For each test case you should output the Mars RGB value in the following format: first output #, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a 0 to its left.

Sample Input:

15 43 71

Sample Output:

#123456

AC的代码

#include<bits/stdc++.h>

using namespace std;

string to13radix(int t){
    map<int, string> d;
    d[10]="A";
    d[11]="B";
    d[12]="C";
    vector<int> num;
    do{
        num.push_back(t%13);
        t/=13;
    }while(t);
    string res;
    for(auto i = num.rbegin();i!=num.rend();i++){
        if((*i)>=10){
            res+=d[(*i)];
        }
        else{
            res+=to_string((*i));
        }
    }
    if(res.size()<2){
        string t(2-res.size(),'0');
        res+=t;
        reverse(res.begin(),res.end());
    }
    return res;
}

int main(){
    int R,G,B;
    cin>>R>>G>>B;
    cout<<'#'+to13radix(R)+to13radix(G)+to13radix(B)<<endl;
       
    return 0;
}
posted @ 2022-01-24 22:48  ghosteq  阅读(33)  评论(0编辑  收藏  举报