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
题目大意:颜色由 R G B 三个字符表示,每个字符对应一个十三进制数。现给出 RGB 三个十进制数,将他们转换位十三进制数
输出以#字符开始
思路:1.由于最后输出的有“#”字符,并且13进制涉数大于9的都用字母表示,因此选择用char 或string类型来存储。
2.因为int类型转char类型比较麻烦,可将单个的char类型当作string类,所以选择string类型
3.十进制转十三进制为连续除13取余并反向输出,为了方便可以用栈临时存储
注意事项: If a single color is only 1-digit long, you must print a 0
to its left. 如果转换的13进制数只有一位,高位用0补齐
#include<iostream>
#include<stack>
#include<vector>
#include<string>
using namespace std;
//将十进制数转换为13进制数
void changeNum(vector<string>&result, int n) {
stack<int> s;//存储转换的13进制数
//如果十进制数为0则将0压入栈中
if (n == 0) {
s.push(0);
}
//连续取余进制转换
while (n != 0) {
int temp = n % 13;
s.push(temp);
n /= 13;
}
//如果13进制数只有一位,则用零补齐
if (s.size() == 1) {
s.push(0);
}
//将13进制数转换为字符串
while (!s.empty()) {
if (s.top() > 9) {
switch (s.top()) {
case 10:result.push_back("A");break;
case 11:result.push_back("B");break;
case 12:result.push_back("C");break;
}
s.pop();
}
else {
result.push_back(to_string(s.top()));
s.pop();
}
}
}
int main() {
vector<string>result;
//3个十进制数,边输入边转换
for (int i = 0;i < 3;i++) {
int n;
cin >> n;
changeNum(result, n);
}
//输出
cout << "#";
for (int i = 0;i < result.size();i++) {
cout << result[i];
}
cout << endl;
return 0;
}