PTA甲级——1027

1027 Colors in Mars

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:

1
15 43 71

Sample Output:

1
#123456

思路

​ 纯纯模拟十三进制和10进制之间转换问题,这里需要注意下,我们在使用to_string函数的时候,参数我们只可以选择int、longlong、double等类型的,但是对于char类型的数据它是不可以用to_string转的,函数会将char类型数据先转换为int类型再进行计算导致结构输出错误,这个时候就建议大家可以选择使用char数组的方式来编写代码了

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstring>

using namespace std;

void toDec(int x) {
char s[3];
int kk = 0;
while (x){
int k = x % 13;
char c = 'A' + k - 10;
// cout << c << '\n';
if (k < 10) s[kk++] = '0' + k;
else s[kk++] = c;
x /= 13;
}
while (kk != 2) s[kk++] = '0';
for (int i = kk - 1; i >= 0; i--) cout << s[i];
// return s;
}
int main()
{
int a, b, c;
cin >> a >> b >> c;
cout << "#";
toDec(a);
toDec(b);
toDec(c);
// cout << "#" << s1 << s2 << s3;
}