PAT 1019 General Palindromic Number (20分) python实现

向前或向后写入时相同的数字称为回文数。例如,1234321是回文数。所有单数数字都是回文数字。虽然回文数在十进制中最常被考虑,但回文性的概念可以适用于任何数字系统中的自然数。考虑基b≥2中的一个数字N>0,其中它是用标准表示法编写的,用k+1位数字(​i​​)作为∑​i=0​k​​(​i​​b​i​​)。在这里,和往常一样,0≤a​i​​<b对于所有i和​k​​是非零的。然后N是回文的当且仅当​i​​=a​k−i​​对于所有i.0在任何基中都是0,并且定义上也是回文的。给定任何正十进制整数N和基b,您应该知道N是否是基b中的回文数。
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and b, where 0<N≤10
​9
​​ is the decimal number and 2≤b≤10
​9
​​ is the base. The numbers are separated by a space.

Output Specification:
For each test case, first print in one line Yes if N is a palindromic number in base b, or No if not. Then in the next line, print N as the number in base b in the form "a
​k
​​ a
​k−1
​​ … a
​0
​​ ". Notice that there must be no extra space at the end of output.

Sample Input 1:
27 2
Sample Output 1:
Yes
1 1 0 1 1
Sample Input 2:
121 5
Sample Output 2:
No

具体思路

就是数制转换,再加上个回文数判断,,很简单,因为数值大,我就用的python

具体代码

def main():
    y=input('').split( )
    ben=int(y[0])
    cha=int(y[1])
    s=[]
    sum=1
    while ben!=0:#基本数制转换方法
        sum=ben%cha
        s.append(sum)
        ben=ben//cha
    j=len(s)-1
    i=0
    biaoji=0
    while j-i>1:#判断是否为回文数
        if(s[j]!=s[i]):
            biaoji=1
        j=j-1
        i=i+1
    if len(s)==2:
        if(s[j]!=s[i]):
            biaoji=1
    if biaoji==1:
        print("No")
    else:
        print("Yes")
    s=s[::-1]#记得逆序输出
    for i in range(len(s)):
        if i!=0:
            print(' ',end='')
        print(str(s[i]),end='')
main()

很简单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值