PAT(甲) 1027 Colors in Mars (20)(詳解)

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.


  • 輸入格式
    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.


題目大意:
這道題目很簡單,就是把10進制數轉爲13進制數,然後輸出。

解題方法:
直接用一個char數組存放對應每個10進制數對應的13進制結果,然後用取餘法,得到13進制數,直接輸出即可。


程序:

#include <stdio.h>

void trans(int N)
{
    int out[2] = {0, 0}, idx = 1;
    char ch[13];    ch[10] = 'A', ch[11] = 'B', ch[12] = 'C';
    for (int i = 0; i < 10; i++)
        ch[i] = i + '0';        /* 初始化 */
    while (N > 0)
    {   /* 取餘法求轉換後的進制 */
        out[idx--] = N % 13;
        N /= 13;
    }
    for (int i = 0; i < 2; i++)  /* 輸出結果 */
        printf("%c", ch[out[i]]);
}

int main(int argc, char const *argv[])
{
    int R, G, B;
    scanf("%d %d %d", &R, &G, &B);
    printf("#");
    trans(R);
    trans(G);
    trans(B);
    return 0;
}

如果對您有幫助,幫忙點個小拇指唄~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章