【2019校招真题】目的地最短步数(python)
目的地最短步数_牛客网
https://www.nowcoder.com/practice/24ec35c2a8054a7b831a5a3ea660d729?tpId=98&tqId=32875&tPage=3&rp=3&ru=/ta/2019test&qru=/ta/2019test/question-ranking
完整代码:
n = int(input()) def calc(n): temp = 0 for i in range(1, n + 1): temp += i if temp == n: return i elif temp > n: t = temp - n if t % 2 == 0: return i else: i += 1 y = t + i temp += i while y % 2 != 0: i += 1 y = t + i temp += i return i return -1 print(calc(n))