WA 求调

P1015 [NOIP1999 普及组] 回文数

HarveyZ @ 2024-09-28 15:46:04

luogu ide上面没问题,但是评测会WA

n = int(input())
m = list(input())[::-1]
count = 0
def add(nm,n):
    lm = len(nm)
    num = [0]*lm
    for i in range(lm):
        num[i] += nm[i]+nm[lm-1-i]
        if num[i] >=n:
            num[i] %= n
            if i != lm-1:
                num[i+1] += 1
            else:
                num.append(1)
    return num
for i in range(len(m)):
    if m[i].isalpha():
        m[i] = ord(m[i])-55
    else:
        m[i] = ord(m[i])-48
while m[::-1] != m:
    m = add(m,n)
    count += 1
print("STEP="+str(count))

by Exschawasion @ 2024-09-28 15:59:19

@HarveyZ

import sys
def tostr(n,base):
    if base==16:
        return hex(n)[2:]
    digits=[]
    while n:
        digits.append(int(n%base))
        n//=base
    return ''.join(str(x) for x in reversed(digits))

n=int(input())
m=input().strip()
count=0

while m!=m[::-1]:
    m=tostr(int(m[::-1],n)+int(m,n),n)
    count+=1
    if count>30:
        print('Impossible!')
        sys.exit(0)

print("STEP="+str(count))

|