Python ZOJ1078 Palindrom Numbers回文数

题目

 

We say that a number is a palindrom if it is the sane when read from left to right or from right to left. For example, the number 75457 is a palindrom.
Of course, the property depends on the basis in which is number is represented. The number 17 is not a palindrom in base 10, but its representation in base 2 (10001) is a palindrom.
The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.

 判断输入的数字是否在某一进制下为回文数

输入输出要求

  • 输入

Several integer numbers comprise the input. Each number 0 < n < 50000 is given in decimal basis in a separate line. The input ends with a zero.
 

 输入多个数字,每行输入一个在(0,5000)内的十进制数,"0"是输入结束的标志

  • 输出

Your program must print the message Number i is palindrom in basis where I is the given number, followed by the basis where the representation of the number is a palindrom. If the number is not a palindrom in any basis between 2 and 16, your program must print the message Number i is not palindrom.
 

 如果输入的数字在2进制到16进制内的进制下是一个回文数,那么打印出这个数字是一个回文数,并且在哪些进制下是回文数

如果输入的数字在任何2进制到16进制内的进制下都不是一个回文数,那么打印“这个数字不是一个回文数”

输入输出样例

 Sample Input
17
19
0
Sample Output
Number 17 is palindrom in basis 2 4 16
Number 19 is not a palindrom 

解题

确保我们的输入输出格式正确:

我们代码的主干应该看起来像这样:

while True:
    n = int(input(""))
    if n == 0:
        break
    #下面则是我们解题的核心部分

进制转换:

很明显我们这里需要进行进制转换,不妨写一个实现输入数字进制转换的函数。当然是使用取余法计算,这样我们可以处理各种十进制向k进制的问题。

def transist(num, basis):#传入的形参分别是输入的数字,以及我们想要转化的进制
    new_num = 0#new_num会是这个函数返回的值
    #要注意到从11进制到16进制的情况其实和从2进制到10进制不尽相同
    if basis <= 10:
        n = 0
        while num != 0:
            r = num % basis
            num = num // basis
            new_num += r * (10 ** n) #当然也可以使用字符串来组成new_num
            n += 1
    else:
        string_1 = ""
        '''
        考虑到十进制以上的数会有字母组成
        我们可以把输入数字转化为n进制(10<n<=16)\的结果一位一位地储存在字符串中
        '''
        while num != 0:
            r = num % basis
            num = num // basis
            if r >= 10:
                r = chr(r + 55)#我们知道A的ASCII码是65,而A代表10
            string_1 += str(r)
            new_num = string_1[::-1]
            '''因为我们每次把余数都放在了字符串的最后端,所以我们这里需要用切片来反转'''
    return new_num

输出部分:

while True:
    string_2 = ""#string_2用于储存a在哪些进制下是回文数
    a = int(input(""))
    if a == 0:
        break
    for i in range(2, 17):
        x = str(transist(a, i))#调用我们函数
        if x == x[::-1]:
            string_2 += " " + str(i)
    if string_2 == "":
        print("Number {} is not a palindrom".format(a))
    else:
        print("Number {} is palindrom in basis".format(a) + string_2)

最终答案:

def transist(num, basis):
    new_num = 0
    if basis <= 10:
        n = 0
        while num != 0:
            r = num % basis
            num = num // basis
            new_num += r * (10 ** n)
            n += 1
    else:
        string_1 = ""
        while num != 0:
            r = num % basis
            num = num // basis
            if r >= 10:
                r = chr(r + 55)
            string_1 += str(r)
            new_num = string_1[::-1]
    return new_num

while True:
    string_2 = ""
    a = int(input(""))
    if a == 0:
        break
    for i in range(2, 17):
        x = str(transist(a, i))
        if x == x[::-1]:
            string_2 += " " + str(i)
    if string_2 == "":
        print("Number {} is not a palindrom".format(a))
    else:
        print("Number {} is palindrom in basis".format(a) + string_2)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桜小路嵐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值