PTA-1027——Colors in Mars

本文介绍了一种将十进制颜色值转换为十三进制火星RGB颜色值的方法。通过使用C++编程语言,详细展示了如何将红、绿、蓝三个通道的十进制数值转换为火星人使用的基于13进制的颜色表示。

题目:

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

分析:

转13进制

代码:

 1 #include<iostream>
 2 using namespace std;
 3 int a,b,c;
 4 char x[2],y[2],z[2];
 5 int main(){
 6     cin>>a>>b>>c;
 7     cout<<"#";
 8     int k=0;
 9     while(a){
10         if(a%13<10){
11             x[k]='0'+a%13;
12         }else{
13             switch(a%13){
14                 case 10:x[k]='A';break;
15                 case 11:x[k]='B';break;
16                 case 12:x[k]='C';break;
17             }
18         }
19         a/=13;
20         k++;
21     }
22     if(k==0){
23         x[0]='0';
24         x[1]='0';
25     }else if(k==1){
26         x[1]='0';
27     }
28     cout<<x[1]<<x[0];
29     k=0;
30     while(b){
31         if(b%13<10){
32             y[k]='0'+b%13;
33         }else{
34             switch(b%13){
35                 case 10:y[k]='A';break;
36                 case 11:y[k]='B';break;
37                 case 12:y[k]='C';break;
38             }
39         }
40         b/=13;
41         k++;
42     }
43     if(k==0){
44         y[0]='0';
45         y[1]='0';
46     }else if(k==1){
47         y[1]='0';
48     }
49     cout<<y[1]<<y[0];
50     k=0;
51     while(c){
52         if(c%13<10){
53             z[k]='0'+c%13;
54         }else{
55             switch(c%13){
56                 case 10:z[k]='A';break;
57                 case 11:z[k]='B';break;
58                 case 12:z[k]='C';break;
59             }
60         }
61         c/=13;
62         k++;
63     }
64     if(k==0){
65         z[0]='0';
66         z[1]='0';
67     }else if(k==1){
68         z[1]='0';
69     }
70     cout<<z[1]<<z[0];
71     return 0;
72 } 

 

 

转载于:https://www.cnblogs.com/orangecyh/p/10371346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值