打印如下的图形。待会我给答案!
1  2    6   7
3  5    8  13
4  9   12  14
10  11  15  16 

解决方案 »

  1.   

    这个前段时间刚遇到过
    可以看这个帖子:http://topic.csdn.net/u/20080226/10/7fb6bd6b-636b-4274-ac0e-63b1d5f46575.html
    我再把解答贴一下吧:
    除了解决楼主的特殊5*5之后,我还考虑了其他的情况,做了个万用型的,还能自己调节上、下转方向,楼主可以参考一下: 
    private void test() {
            int line = 9;    // 行数
            int row = 6;    // 列数
            String orderBy = "desc";    // 上、下转方向
            
            int bias = line + row - 1;    // 斜线数
            int currentBiasInclude = 0;
            int toMaxBiasNumberIndex = Math.min(line, row) - 1;
            String[][] result = new String[line][row];
            int k = 1;
            for (int i = 0; i < bias; i++) {
                if (i <= toMaxBiasNumberIndex) {
                    currentBiasInclude++;
                } else if (i >= bias - toMaxBiasNumberIndex) {
                    currentBiasInclude--;
                }
                
                if (orderBy.equals("asc")) {
                    for (int j = 0,x = Math.min(i, line - 1), y = i <= (line-1) ? 0 : i - ((line-1)); j < currentBiasInclude; j++,x--,k++,y++) {
                        result[x][y] = "" + k;
                    }
                    orderBy = "desc";
                } else {
                    for (int j = currentBiasInclude, x = i <= (row-1) ? 0 : i - ((row-1)),y = Math.min(i, row - 1); j > 0; j--,x++,k++,y--) {
                        result[x][y] = "" + k;
                    }
                    orderBy = "asc";
                }
            }
            for(int i = 0; i < result.length; i++){
                System.out.println();
                for (int j = 0; j < result[i].length; j++) {
                    System.out.print(result[i][j] + "   ");
                    if(result[i][j].length()==1){
                        System.out.print(" ");
                    }
                }
            }
        }