蛇形矩阵
蛇形矩阵
http://www.nowcoder.com/questionTerminal/649b210ef44446e3b1cd1be6fa4cab5e
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int num = sc.nextInt(); int[][] dp = new int[num][num]; int index = 1; for(int level = 1; level <= num; ++level){ int i = level - 1; int j = 0; while((i >= 0) && (j <= level - 1)){ dp[i][j] = index; i--; j++; index++; } } for(int i = 0; i < num; ++i){ for(int j = 0; j < num - i; ++j){ System.out.print(dp[i][j] + " "); } System.out.println(); } } } }