• PAT 1027 Colors in Mars


    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:

    15 43 71
    

    Sample Output:

    #123456

    总结:这题还是很简单的,没有太多的坑(坑的地方也已经说明了),直接将十进制数转换成十三进制数就行了

     这里比较好的处理方式是将所有结果的数字都存储在char类型的数组中,这样可以减少很多不必要的判断

    1. #include
    2. using namespace std;
    3. int main(){
    4. int a[3];
    5. char digit[]={'0','1','2','3','4','5','6','7','8','9','A','B','C'};
    6. cin >> a[0] >> a[1] >> a[2];
    7. printf("#");
    8. for(int i=0;i<3;i++){
    9. string s;
    10. if(a[i]>=13) s+=(digit[a[i]/13]);
    11. else s+='0';
    12. s+=(digit[a[i]%13]);
    13. cout << s;
    14. }
    15. return 0;
    16. }

    依照惯例,看看大佬的代码:

    果然,还是被打击了,真的是太厉害了,膜拜!

    1. #include
    2. using namespace std;
    3. int main() {
    4. char c[14] = {"0123456789ABC"};
    5. printf("#");
    6. for(int i = 0; i < 3; i++) {
    7. int num;
    8. scanf("%d", &num);
    9. printf("%c%c", c[num/13], c[num%13]);
    10. }
    11. return 0;
    12. }

    好好学习,天天向上!

    我要考研

    2022.11.2

    1. #include
    2. using namespace std;
    3. int main(){
    4. int a,b,c;
    5. scanf("%d%d%d",&a,&b,&c);
    6. char color[]="0123456789ABC";//0~12
    7. printf("#%c%c",color[a/13],color[a%13]);
    8. printf("%c%c",color[b/13],color[b%13]);
    9. printf("%c%c",color[c/13],color[c%13]);
    10. return 0;
    11. }

  • 相关阅读:
    算法:(二)数组
    防火墙基础之H3C防火墙分支与分支之间双向地址转换
    Golang 通道 channel
    ASCHIP_ISP Tool 工具 使用与更新
    我们为什么做NDH
    Flutter 错误must be a valid Dart package name
    嵌入式学习笔记(47)Nand的常见操作及流程分析
    【c语言】编译链接--详解
    选择最适合你的图像分类模型
    Hello Vue!
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/126855266