a[6]={1,2,3,4,5,6} 存储的是4行4列对称数组的上三角元素,数值对应位置参照下面,
反得到数组A[4][4] 如下,对角线元素均为0
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
各位高手,怎么实现??
------解决方案--------------------
for example
- Java code
public class csdn { public static void main(String[] args) throws Throwable { int n = 4; int[][] matrix = new int[n][n]; for (int i=0, cnt=1; i<n; i++) { for (int j=i; j<n; j++) { if (i==j) {matrix[i][j] = 0;} else { matrix[i][j] = cnt; matrix[j][i] = cnt; cnt++; } } } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { System.out.printf("%2d ", matrix[i][j]); } System.out.println(); } }}
------解决方案--------------------