小美的排列构造 python
''' 这道题的思路就是从一个1到n的数组中,重新排列,数字组间和之差最小。最理想的方案就是每次只取数组中的最大值和最小值. 最终就会得到这样的一个数组[n,1, n-1,2, n-2,3, ..., n//2],然后将数组逆序转换成字符串 ''' n = int(input()) res = [0 for i in range(n)] max_n = n min_n = 1 for i in range(0, n, 2): res[i] = max_n max_n -= 1 for i in range(1, n, 2): res[i] = min_n min_n += 1 out = " ".join([str(r) for r in res[::-1]]) print(out)
题目链接:https://www.nowcoder.com/questionTerminal/bd44aa9a468144b6b53391ae56f2d730