1027 Colors in Mars (20)(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
Each input file contains one test case which occupies a line containing the three decimal color values.
Output
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 the left.
Sample Input
15 43 71
Sample Output
#123456
题解1:
把读入的每个十进制数转换成13进制数,依次输出。这里看到只有3个数,就偷懒没有写循环,结果自己粗心大意没有把数组下标改好,浪费了许多时间。
源代码1:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
char res[8] = {"#000000"};
cin >> a >> b >> c;
if (a / 13 <= 9) res[1] = a / 13+'0';
else res[1] = a / 13 - 10 + 'A';
if (b / 13 <= 9) res[3] = b / 13 + '0';
else res[3] = b / 13 - 10 + 'A';
if (c / 13 <= 9) res[5] = c / 13 + '0';
else res[5] = c / 13 - 10 + 'A';
if (a % 13 <= 9) res[2] = a % 13 + '0';
else res[2] = a % 13 - 10 + 'A';
if (b % 13 <= 9) res[4] = b % 13 + '0';
else res[4] = b % 13 - 10 + 'A';
if (c % 13 <= 9) res[6] = c % 13 + '0';
else res[6] = c % 13 - 10 + 'A';
for (int i = 0; i < 7; i++)
cout << res[i];
return 0;
}
题解2:
把13进制数的每个数存入一个数组中,每次计算得到一个数,就输出该数组的该位数。
源代码2:
#include <iostream>
using namespace std;
int main()
{
int a[3];
char s[14]={"0123456789ABC"};
for(int i=0;i<3;i++)
cin>>a[i];
cout<<"#";
for(int i=0;i<3;i++)
cout<<s[a[i]/13]<<s[a[i]%13];
return 0;
}