A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


n
个整数,使其前面各数顺序向后移
m
个位置,最后
m
个数变成最前面的
m
个数

1 个回复

倒序浏览
  1. public static void main(String[] args) {
  2.                 int shift = 3;
  3.                 int[] arr;

  4.                 arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  5.                 shiftLeft(arr, shift);
  6.                 System.out.printf("向左移动%d %s%n", shift, Arrays.toString(arr));

  7.                 arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  8.                 shiftRight(arr, shift);
  9.                 System.out.printf("向右移动%d %s%n", shift, Arrays.toString(arr));
  10.         }

  11.         /**
  12.          * 向右移动 要求 arr != null && arr.length >= 1 && shift >= 0
  13.          */
  14.         public static void shiftRight(int[] arr, int shift) {
  15.                 if ((shift %= arr.length) == 0) {
  16.                         return;
  17.                 }

  18.                 int len = arr.length;
  19.                 int step = GCD(len, shift);
  20.                 int round = len / step;

  21.                 for (int i = 0; i < step; i++) {
  22.                         int t = arr[(i + shift * round) % len], index1 = 0, index2 = 0;
  23.                         for (int j = round; j > 1; j--) {
  24.                                 index2 = (i + shift * j - shift) % len;
  25.                                 index1 = (index2 + shift) % len;

  26.                                 arr[index1] = arr[index2];
  27.                         }
  28.                         arr[index2] = t;
  29.                 }
  30.         }

  31.         /**
  32.          * 向左移动 要求 arr != null && arr.length >= 1 && shift >= 0
  33.          */
  34.         public static void shiftLeft(int[] arr, int shift) {
  35.                 if ((shift %= arr.length) == 0) {
  36.                         return;
  37.                 }

  38.                 int len = arr.length;
  39.                 int step = GCD(len, shift);
  40.                 int round = len / step;

  41.                 for (int i = 0; i < step; i++) {
  42.                         int t = arr[i], index1 = 0, index2 = 0;
  43.                         for (int j = 0; j < round - 1; j++) {
  44.                                 index1 = (i + shift * j) % len;
  45.                                 index2 = (index1 + shift) % len;

  46.                                 arr[index1] = arr[index2];
  47.                         }
  48.                         arr[index2] = t;
  49.                 }
  50.         }

  51.         /**
  52.          * 辗转相除法求最大公约数 要求 a > 0 && b > 0
  53.          */
  54.         public static int GCD(int a, int b) {
  55.                 while (b != 0) {
  56.                         int t = a;
  57.                         a = b;
  58.                         b = t % b;
  59.                 }
  60.                 return a;
  61.         }
复制代码


如果您觉得我的回答还满意的话,请回复一下我
我的QQ:2355928351
如果您还有其他的入学前问题可以加我的QQ
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马