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] );
相关文章推荐
- C语言进制转换代码分享
- C 语言进制之间的转换
- PHP中实现中文字符进制转换原理分析
- php 实现进制相互转换
- JS中字符问题(二进制/十进制/十六进制及ASCII码之间的转换)
- php 实现进制转换(二进制、八进制、十六进制)互相转换实现代码
- javascript parseInt() 函数的进制转换注意细节
- 进制转换算法原理(二进制 八进制 十进制 十六进制)
- Shell脚本中不同进制数据转换的例子(二进制、八进制、十六进制、base64)
- 使用JavaScript进行进制转换将字符串转换为十进制
- Javascript进制转换实例分析
- javascript常见数字进制转换实例分析
- JS中的进制转换以及作用
- Python中不同进制的语法及转换方法分析
- Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
- php 实现进制相互转换
- javascript常见数字进制转换实例分析
- Python中不同进制的语法及转换方法分析
- 十六进制转换八进制