36 有n个整数,使其前面各数顺序向后移n个位置,最后m个数变
2023-10-23 本文已影响0人
北极的大企鹅
题目:有n个整数,使其前面各数顺序向后移n个位置,最后m个数变成最前面的m个数
1 public class _036ExchangeSite {
2
3 public static void main(String[] args) {
4 exchangeSite();
5 }
6
7 private static void exchangeSite() {
8 int N = 10;
9 int[] a = new int[N];
10 Scanner scanner = new Scanner(System.in);
11
12 System.out.println("请输入10个整数 :");
13 for (int i = 0; i < N; i++) {
14 a[i] = scanner.nextInt();
15 }
16
17 System.out.println("你输入的数组为 :");
18 for (int i = 0; i < N; i++) {
19 System.out.print(a[i] + " ");
20 }
21
22 System.out.println("\n请输入向后移动的位数 :");
23 int m = scanner.nextInt();
24 int[] b = new int[m];
25 for (int j = 0; j < m; j++) {
26 b[j] = a[N - m + j];
27 }
28
29 for (int k = N - 1; k >= m; k--) {
30 a[k] = a[k - m];
31 }
32
33 for (int l = 0; l < m; l++) {
34 a[l] = b[l];
35 }
36
37 System.out.println("位移后的数组是 :");
38 for (int n = 0; n < N; n++) {
39 System.out.print(a[n] + " ");
40 }
41 }
42 }