核心是数组的右移
import java.util.Scanner;
public class Algorithm01 {
/**
* 数组的右移
* @param c 传递和右移数组
* @param i 右移的次数
* @return 返回移动后的数组
*/
public static int[] ArrayRight(int[] c, int i) {
int temp;
for (int j = 0; j < i; j++) {
temp = c[c.length - 1];
for (int k = c.length - 2; k >= 0; k--) {
c[k + 1] = c[k];
}
c[0] = temp;
}
return c;
}
public static void main(String[] args) {
int n = 6;
int[] a = new int[6];
int[] b = new int[6];
Scanner sc = new Scanner(System.in);
System.out.println("输入数组:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
if (i == 5)
System.out.println();
}
for (int i = 0; i < n-1; i++) {
//将数组每次右移一位
//由于定义的是静态方法,a数组会改变,所以每次移动一位
b = ArrayRight(a, 1);
for (int j = 0; j < n; j++) {
System.out.print(b[j]+" ");
if (j == 5)
System.out.println();
}
}
}
}