您的位置:首页 > 其它

PAT甲级.1027. Colors in Mars (20)

2016-08-20 11:19 447 查看

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.

输入格式:

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

输出格式:

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.

输入样例:

15 43 71

输出样例:

#123456

题意:

将3个【0,168】范围内的10进制数转化为13进制数输出

思路:

1.用char raadix13[13]数组来存要用的字符:

char radix13[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A','B','C'};


2.除基取余转换为对应数组输出

for(int i = 0; i<3; i++)
{
int num = 0;    //num初始化要放在do外面
do
{
tRGB[i][num++] = radix13[dRGB[i] % 13]; //除基取余
dRGB[i] /= 13;
} while (dRGB[i] != 0);
}


代码:

version1.0

/**
* @tag     PAT_A_1027
* @authors R11happy (xushuai100@126.com)
* @date    2016-8-19 23:29-23:57
* @version 1.0
* @Language C++
* @Ranking  260/3135
* @function null
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>

int main(int argc, char const *argv[])
{
char tRGB[3][2];   //tRGB[0][2]-R  tRGB[1][2]-G    tRGB[2][2]-B
//不能直接放全局,因为会初始化为\0,而不是字符'0'
for (int i = 0; i < 3; ++i)
{
for(int j = 0; j<2; j++)
tRGB[i][j] = '0';
}
char radix13[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A','B','C'};
int dRGB[3];    //dRGB[0]-R  dRGB[1]-G  dRGB[2]-B
scanf("%d%d%d", &dRGB[0], &dRGB[1], &dRGB[2]);
for(int i = 0; i<3; i++)
{
int num = 0;    //num初始化要放在do外面
do
{
tRGB[i][num++] = radix13[dRGB[i] % 13]; //除基取余
dRGB[i] /= 13;
} while (dRGB[i] != 0);
}
printf("#");
for(int i = 0; i<3; i++)
for(int j = 1; j>=0; j--)
{
printf("%c",tRGB[i][j] );
}
return 0;
}


version2.0

/**
* @tag     PAT_A_1027
* @authors R11happy (xushuai100@126.com)
* @date    2016-8-19 23:29-
* @version 1.0
* @Language C++
* @Ranking  null
* @function null
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>

int main(int argc, char const *argv[])
{
char radix13[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A','B','C'};
int r, g, b;
scanf("%d%d%d", &r, &g, &b);
printf("#");
printf("%c%c",radix13[r/13], radix13[r%13] );
printf("%c%c",radix13[g/13], radix13[g%13] );
printf("%c%c",radix13[b/13], radix13[b%13] );
return 0;
}


收获:

1.char类型放全局变量会自动初始化为’\0’而不是’0’,要初始化需要自己赋值

2.进制中带A,B,C这种的可以考虑用一个数组来存

3.转换成的进制如果位数已知且位数较少的情况下可以考虑直接计算输出而不需要用除基取余。

printf("%c%c",radix13[r/13], radix13[r%13] );
printf("%c%c",radix13[g/13], radix13[g%13] );
printf("%c%c",radix13[b/13], radix13[b%13] );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  进制转换