网易-算法工程师-笔试
有没有人讨论一下网易今天的笔试题,1-n正整数字典序排列,根据给出的第q个正数数列,算出第q个倒数数列。 Python实现的小伙伴来指点一下呗。谢谢惹。 n = int(input()) q = [int(x) for x in input().split()] nums = [i for i in range(1, n+1)] output = [] #计算出是正数第几个 res = 0 for i in range(n): digit = q[i] count = 1 for j in range(2, n-i): count *= j index = nums.index(digit) nums.pop(index) if i == n-1: res += (index+1) * count else: res += index * count nums = [i for i in range(1, n+1)] for i in range(n-1,0,-1): count = 1 for j in range(2, i+1): count *= j digit = res // count res %= count if digit == 0 and res == 0: break elif res == 0: output.append(nums.pop(-digit)) else: output.append(nums.pop(-digit-1)) output.extend(nums) output = [str(x) for x in output] print(' '.join(output))
#网易##笔试题目##算法工程师#