本文共 727 字,大约阅读时间需要 2 分钟。
为了解决这个问题,我们需要将数组中的每个整数循环向右移M个位置,同时尽量减少移动数据的次数。通过数学优化,我们可以减少实际的移位次数,从而提高效率。
n, m = map(int, input().split())a = list(map(int, input().split()))d = m % nif d == 0: print(' '.join(map(str, a)))else: new_a = [] for j in range(n): pos = (j - d + n) % n new_a.append(str(a[pos])) print(' '.join(new_a)) 这种方法通过数学优化减少了移位次数,确保了在处理大M值时的效率。
转载地址:http://uwesz.baihongyu.com/