输入一个数  比如说 5
 输出
 1  3  4  10  11
 2  5  9  12  19
 6  8  13  18  20
 7  14  17 21 24
 15 16 22  23 25
如何实现

解决方案 »

  1.   

    多用几个for循环和判断,很容易写出来
      

  2.   


    不是的,就是输入n,然后输出 n*n的二维数组,N自己输入
      

  3.   


         public static void method(int len) {
            int[][] data = new int[len][len];
            boolean flag = false;
            int x = 0, y = 0;
            for (int i = 1; i <= len * len; i++) {
                if (x < len && y < len) {
                    data[x][y] = i;
                } else {
                    i--;
                }
                if ((flag && x == 0) || (!flag && y == 0)) {
                    if (flag) {
                        y++;
                    } else {
                        x++;
                    }
                    flag = !flag;
                } else {
                    if (flag) {
                        y++;
                        x--;
                    } else {
                        y--;
                        x++;
                    }
                }
            }
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    System.out.print(data[i][j]);
                    System.out.print(" ");
                }
                System.out.print("\n");
            }
        }
    随便写一个,你自己改改。
      

  4.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;/*输入一个数  比如说 5
     输出
     1  3  4  10  11
     2  5  9  12  19
     6  8  13  18  20
     7  14  17 21 24
     15 16 22  23 25*/public class ArrayTest { public static void main(String[] args) throws NumberFormatException,
    IOException { InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    System.out.println("please input a number");
    int n;
    n = Integer.parseInt(br.readLine());
    System.out.println(n); method(n);
    } private static void method(int n) {
    int[][] arrayOut = new int[n][n];

    int r=0;int c=0;

    for(int i=1;i<n*n;i++){
    if(r<n&&c<n){
    arrayOut[r][c]=i;
    }
    else{
    i--;
    }

    if(c==0&&r%2==0){
    r++;
    }
    else{
    if(r==0&&c%2!=0){
    c++;
    }
    else{
    if((c+r)%2==0){
    r++;
    c--;
    }
    else{
    r--;
    c++;
    }
    }
    }


    }

    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    System.out.print(arrayOut[i][j]+" ");
    }
    System.out.println();
    }

    }
    }
    参照你的写的,但是为什么出来后最后一个数总是为0?
      

  5.   

      public static void main(String[] args) throws Exception {
        int[][] array = array(5);
        for (int i = 0; i < array.length; i++) {
          for (int j = 0; j < array[i].length; j++) {
            System.out.printf("%5d\t", array[i][j]);
          }
          System.out.println();
        }
      }  static int[][] array(int n) {
        int[][] array = new int[n][n];
        int count = 0;
        boolean xFirst = false;
        int max = (n - 1) << 1;
        for (int sum = 0; sum <= max; sum++, xFirst = !xFirst) {
          for (int i = 0; i <= sum; i++) {
            try {
              int x = xFirst ? i : sum - i;
              int y = sum - x;
              array[y][x] = ++count;
            } catch (Exception e) {
            }
          }
        }
        return array;
      }
      

  6.   

            try {
              int x = xFirst ? i : sum - i;
              int y = sum - x;
              array[y][x] = count+1;
              count++;
            } catch (Exception e) {
            }
      

  7.   


    看下面,flag为true的时候x--,所以到0的时候不能继续减了,开始换方向
    另写的时候比较仓促,没有考虑flag的具体含义。