Palindromic Squares

题目描述:

Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

输入输出格式:

INPUT FORMAT

A single line with B, the base (specified in base 10).

SAMPLE INPUT (file palsquare.in)

10

OUTPUT FORMAT

Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!

SAMPLE OUTPUT (file palsquare.out)

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696

题目大意:

输入一个(2-20)进制数,判断这个数(1-300范围)的平方是不是回文数,所回文数是指121,从左到右和从右到左是一样的数,输入的哪一行是数的进制,

解题思路:

这是一道水题,注意一点就行,10进制转任意进制用辗转相除法,具体过程看我代码的f(x,n)外带我用python写的,基本没啥解题思路,直接暴力就完事,解题代码如下

"""
ID: scwswx2
LANG: PYTHON3
TASK: palsquare
"""
def f(n,x):
    #n为待转换的十进制数,x为进制,取值为2-20
    a=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J']
    b=[]
    wx=''
    while True:
        s=n//x#商
        y=n%x#余数
        b=b+[y]
        if s==0:
            break
        n=s
    b.reverse()
    for i in b:
        #print(a[i],end='')
        wx=wx+str(a[i])
    #print(wx)
    return wx
fin=open('palsquare.in','r')
fout=open('palsquare.out','w')
a=int(fin.readline().strip('\n'))#输入的进制数
for i in range(1,300):
    before=f(i,a)
    palsquareBefor=f(int(before,a)*int(before,a),a)
    palsquareAfter=palsquareBefor[::-1]
    if palsquareBefor == palsquareAfter:
        #print(before,palsquareBefor)
        fout.write(before+' '+palsquareBefor+'\n')
fout.close()

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值