这段python代码在洛谷中运行结果为re,请帮我修改一下def toInt(n): if n.islower(): return ord(n) - 87 elif n.isupper(): return ord(n) - 55 return int(n) def toStr(n): if n < 10: return str(n) return chr(n + 87) N = int(input()) M = str(input()) sEd = 0 for i in range(0, 30): memory = "" memoryAdd = 0 for j in range(len(M)): memory += toStr((toInt(M[j]) + toInt(M[len(M) - 1 - j]) + memoryAdd) % N) memoryAdd = (toInt(M[j]) + toInt(M[len(M) - 1 - j]) + memoryAdd) // N if memoryAdd: memory += toStr(memoryAdd) M = memory[::-1] if M == memory: sEd = 1 print("STEP={0}".format(i + 1), end="") break if sEd == 0: print("Impossible!", end="")
时间: 2024-03-25 10:38:22 浏览: 81
Python凯撒密码笔记:原理与实践.md
这段代码在洛谷中运行结果为 "re",这是因为程序在输出答案时,没有加上换行符。
可以在 print 函数中加上参数 `end="\n"`,表示以换行符结尾,即可解决该问题。修改后的代码如下:
```python
def toInt(n):
if n.islower():
return ord(n) - 87
elif n.isupper():
return ord(n) - 55
return int(n)
def toStr(n):
if n < 10:
return str(n)
return chr(n + 87)
N = int(input())
M = str(input())
sEd = 0
for i in range(0, 30):
memory = ""
memoryAdd = 0
for j in range(len(M)):
memory += toStr((toInt(M[j]) + toInt(M[len(M) - 1 - j]) + memoryAdd) % N)
memoryAdd = (toInt(M[j]) + toInt(M[len(M) - 1 - j]) + memoryAdd) // N
if memoryAdd:
memory += toStr(memoryAdd)
M = memory[::-1]
if M == memory:
sEd = 1
print("STEP={0}".format(i + 1), end="")
break
if sEd == 0:
print("Impossible!", end="")
```
阅读全文