解题思路:
注意事项:
参考代码:
import java.util.Scanner; public class 蛇形矩阵 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //蛇形矩阵 Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int [][] a=new int [n] [n]; a[0][0]=1; int b=2; for (int i = 1; i < a.length; i++) { a[0][i]=a[0][i-1]+b; b++; } for (int i = 1; i < a.length; i++) { for (int j = 0; j < a.length-1; j++) { a[i][j]=a[i-1][j+1]-1; } } for (int i = 0,m=n; i < a.length; i++,m--) { for (int j = 0; j < m; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } }
0.0分
0 人评分