编程语言
首页 > 编程语言> > (Python实现)PAT 1027 Colors in Mars (20 分)

(Python实现)PAT 1027 Colors in Mars (20 分)

作者:互联网

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.
火星上的人用与地球人相似的方式在他们的电脑里表示颜色。也就是说,一种颜色由6位数字表示,前2位是“红”,中间2位是“绿”,后2位是“蓝”。唯一的区别是它们使用基数13(0-9和A-C)而不是16。现在给定三个十进制数字的颜色(每个数字在0到168之间),您应该输出它们的Mars RGB值。

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.
对于每个测试用例,应该以以下格式输出Mars RGB值:首先输出’ # ‘,然后是一个6位数字,其中所有的英文字符都必须是大写字母。如果单个颜色只有1位数字长,则必须在其左侧打印一个’ 0 '。

Sample Input:

15 43 71

Sample Output:

#123456

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB

解题思路:
就是每一种情况列出来。分两种情况判断,一种是小于13的,另一种是大于13的。利用到了字符串的“+”

代码:

def trans_10_13(x):
    if x < 10:
        x = str(x)
    elif x == 10:
        x = 'A'
    elif x == 11:
        x = 'B'
    elif x == 12:
        x = 'C'
    return x


def trans(y):
    y = int(y)
    a = ''
    b = ''
    if y < 13:
        a = '0'
    else:
        a = y // 13
        a = trans_10_13(a)
    b = y % 13
    b = trans_10_13(b)
    return a+b


num = input().split(' ')
res = ''
for x in num:
    res += trans(x)
print ("#%s" % res)

提交记录:
在这里插入图片描述

标签:10,1027,PAT,Python,13,color,RGB,Mars,trans
来源: https://blog.csdn.net/qq_51771374/article/details/113851962