题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
Java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int[][] sna=new int[n][n]; sna=snake(n); for(int i=0;i<n;i++){ for(int j=0;j<(n-i);j++){ System.out.print(sna[i][j]+" "); } System.out.println(""); } } } public static int[][] snake(int n){ int[][] s=new int[n][n]; int j=1; for(int sum=0;sum<n;sum++){ for(int i=sum;i>=0;i--){ s[i][sum-i]=j; j++; } } return s; } }