发现好像对称矩阵可以完美符合题意,就先保证对角线及对角线右边不重复,然后把对角线左边补上。本人是菜鸟有不对的地方欢迎大佬指正
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int arr[][]=new int[n][n]; int a =1; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { arr[i][j]=a; a++; }
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
arr[i][j]=arr[j][i];
}
}
arr[n-1][n-1]=a+1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}