Dual Palindromes

本篇介绍了一个编程问题的解决方法,即找出大于指定数值的前N个数,在2到10进制中至少有两种进制下是回文数。文章提供了Python实现代码,展示了如何将十进制数转换为不同进制,并检查其是否为回文。

题目描述:

A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).

Write a program that reads two numbers (expressed in base 10):

  • N (1 <= N <= 15)
  • S (0 < S < 10000)

and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).

Solutions to this problem do not require manipulating integers larger than the standard 32 bits.

输入输出格式:

INPUT FORMAT

A single line with space separated integers N and S.

SAMPLE INPUT (file dualpal.in)

3 25

OUTPUT FORMAT

N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.

SAMPLE OUTPUT (file dualpal.out)

26
27
28

题目大意:

输入一个N,S,输出大于S的前n个(1-10)进制至少有2种进制相同的那个数,21的二进制数就是一个回文数

解题思路:

直接按条件搜索就行,不会出现超时的问题,亲测,问题在于一定要注意循环条件,我被坑了4-5次,就是

for i in range(2,12):这里,一定要注意
"""
ID: scwswx2
LANG: PYTHON3
TASK: dualpal
"""
def f(n,x):
    #n为待转换的十进制数,x为进制,取值为2-20
    a=['0','1','2','3','4','5','6','7','8','9','0']
    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('dualpal.in','r')
fout=open('dualpal.out','w')
inputA,inputB=map(int,fin.readline().split())#输入的N,S
count = 0#记录找到了几个数字
while count<inputA:
    isdual=0
    inputB = inputB + 1
    for i in range(2,12):
        if isdual>=2:
            #print(inputB)
            fout.write(str(inputB)+'\n')
            count=count+1
            break
        if i>=10:
            before=str(inputB)
        before=f(inputB,i)
        after=before[::-1]
        if before==after:
            isdual=isdual+1
fout.close()

 

为了实现从文件读取两个十进制数 `N` 和 `S`,找出前 `N` 个大于 `S` 且在 2 - 10 进制中至少两种进制为回文数的十进制数并输出到文件,同时不使用大于 32 位的整型,可以按照以下思路编写 Python 程序: ```python def is_palindrome(num_str): return num_str == num_str[::-1] def is_multibase_palindrome(num): palindrome_count = 0 for base in range(2, 11): num_in_base = "" temp = num while temp: num_in_base = str(temp % base) + num_in_base temp //= base if is_palindrome(num_in_base): palindrome_count += 1 if palindrome_count >= 2: return True return False # 从文件读取 N 和 S with open('input.txt', 'r') as file: N, S = map(int, file.readline().split()) # 找出满足条件的数 count = 0 result = [] num = S + 1 while count < N: if is_multibase_palindrome(num): result.append(num) count += 1 num += 1 # 将结果输出到文件 with open('output.txt', 'w') as file: for num in result: file.write(str(num) + '\n') ``` ### 代码解释 1. **`is_palindrome` 函数**:该函数用于判断一个字符串是否为回文串,通过比较字符串与其反转后的字符串是否相等来实现。 2. **`is_multibase_palindrome` 函数**:该函数用于判断一个十进制数在 2 - 10 进制中是否至少有两种进制为回文数。对于每个进制,将十进制数转换为该进制的字符串,然后调用 `is_palindrome` 函数进行判断。如果满足条件的进制数达到 2 个或以上,则返回 `True`。 3. **读取输入文件**:使用 `open` 函数打开 `input.txt` 文件,读取第一行的两个整数 `N` 和 `S`。 4. **寻找满足条件的数**:从 `S + 1` 开始遍历,调用 `is_multibase_palindrome` 函数判断每个数是否满足条件。如果满足条件,则将其添加到结果列表中,直到找到 `N` 个数为止。 5. **输出结果到文件**:使用 `open` 函数打开 `output.txt` 文件,将结果列表中的每个数逐行写入文件。 ### 注意事项 - 输入文件 `input.txt` 应包含两个整数 `N` 和 `S`,用空格分隔。 - 输出文件 `output.txt` 将包含找到的 `N` 个满足条件的十进制数,每个数占一行。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值