1027 Colors in Mars (20 分)进制转换

1027 Colors in Mars (20 分)

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

思路:
  主要考虑输入全为0的情况,有一个测试用例是全为0
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stdlib.h>
using namespace std;

string to13(int num)
{
    string temp="";
    while(num!=0)
    {
        int a=num%13;
        char c;
        if(a<10)
            c=a+'0';
        else if(a==10)
            c='A';
        else if(a==11)
            c='B';
        else if(a==12)
            c='C';
        temp+=c;
        num/=13;
    }
    //cout<<temp<<endl;
    reverse(temp.begin(),temp.end());
    if(temp.size()==0)
        return "00";
    else if(temp.size()==1)
        return "0"+temp;
    else 
        return temp;
}


int main()
{
   int r,g,b;
   cin>>r>>g>>b;
   string temp="#"+to13(r)+to13(g)+to13(b);
    cout<<temp<<endl;
    return 0;
}

 

 

posted on 2019-01-20 20:03  ZhangのBlog  阅读(199)  评论(0编辑  收藏  举报