题解 | #[NOIP1999]回文数#
[NOIP1999]回文数
https://www.nowcoder.com/practice/a432eb24b3534c27bdd1377869886ebb
x ={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9, 'A':10,'B':11,'C':12,'D':13,'E':14,'F':15} x_c = ['0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F'] def hui(s): flag = 0 l = int(len(s)/2) for i in range(l): if s[i] != s[len(s)-i-1]: flag = 1 break return flag def fun(c,n): res = [] temp = 0 ct = 0 loop = len(str(c)) num1 = list(c) num2 = num1[::-1] for i in range(loop): temp = x[num1[i]]+x[num2[i]]+ct if temp >= n: temp -= n ct = 1 else: ct = 0 res.append(x_c[temp]) if ct == 1: res.append(x_c[ct]) ans = ''.join(res) ans = ans[::-1] return ans n = int(input()) m = input() count = 0 while hui(m) and count <= 30: m = fun(m,n) count +=1 if count <= 30: print('STEP={}'.format(count)) else: print('Impossible!')
定义一个函数判断字符串是否是回文
定义一个函数实现任意进制互反相加,输出仍是一个字符
主程序循环判断当前字符串是否为回文,不是互反相加再判断