由键盘输入一个数n然后
输出n行n列的循环例如:
输入则输出:
    1  2  3   4
   12 13 14  5
   11 16 15  6
   10  9  8  7
算法怎么写啊,写不出来头疼

解决方案 »

  1.   


    刚才找了一个,是C++的,不过很久没用C++忘了,算法自己去看吧.#include <cstdio>
    void MatrixSpiralOutput(int n)
    {
        int **matrix = new int*[n]();
        for (int idx = 0; idx < n; idx++)
        {
            matrix[idx] = new int[n]();
        }    int row = 0, col = 0;
        int i = 0;
        int len = n * n;
        int circle = 0;    while (i < len)
        {
            for( ;row < n - circle; row++)
                matrix[row][col] = ++i;        row--;
            col++;        for( ; col < n - circle; col++) 
                matrix[row][col] = ++i;        row--;
            col--;        for( ; row >= circle; row--) 
                matrix[row][col] = ++i;        row++;
            col--;        for( ;col > circle; col--) 
                matrix[row][col] = ++i;        row++;
            col++;        circle++;
        }    printf("\n   The  Array matrix[%d][%d] is :", n, n);
        for(int k = 0; k < n; k++)
        {
            printf("\n\n      ");
            for(int j = 0; j < n; j++)
                printf("%-5d", matrix[k][j]);
        }
        printf("\n\n");    for(int idx = 0 ; idx < n ; idx++)
            delete [] matrix[idx];
        delete [] matrix;}
      

  2.   


    public class Test {
      public static void main(String[] args) {
        SpiralMatrix(5);
      }
      /**
       * 类似爬迷宫
       * @param count
       */
      static void SpiralMatrix(int count)
      {
          int round = count - 1;
          int[][] matrix = new int[count][count];
          int num = 1;
          for (int r = 0; r < round && num <= count * count; r++)
          {
              for (int tj = r; tj <= round - r; tj++)// top
                  matrix[r][tj] = num++;
              for (int ri = r + 1; ri <= round - r; ri++) // right
                  matrix[ri][ round - r] = num++;
              for (int bj = round - r - 1; bj >= r; bj--)// bottom
                  matrix[round - r][bj] = num++;
              for (int li = round - r - 1; li > r; li--)// left
                  matrix[li][r] = num++;
          }
          for (int j = 0; j < matrix.length; j++) {
            for (int i = 0; i < matrix[j].length; i++) {
              System.out.print(matrix[j][i]+" ");
            }
            System.out.println("");
          }
      }
    }输出如下:
    1  2  3  4  5 
    16 17 18 19 6 
    15 24 25 20 7 
    14 23 22 21 8 
    13 12 11 10 9 
      

  3.   

    import java.io.*;public class Text{
      public static void main(String[] args)throws IOException{
      String a="";
      int c;
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      a = br.readLine();
      c = Integer.parseInt(a);
      SpiralMatrix(c);
      }
      /**
       * 类似爬迷宫
       * @param count
       */
      static void SpiralMatrix(int count)
      {
          int round = count - 1;
          int[][] matrix = new int[count][count];
          int num = 1;
          for (int r = 0; r < round && num <= count * count; r++)
          {
              for (int tj = r; tj <= round - r; tj++)// top
                  matrix[r][tj] = num++;
              for (int ri = r + 1; ri <= round - r; ri++) // right
                  matrix[ri][ round - r] = num++;
              for (int bj = round - r - 1; bj >= r; bj--)// bottom
                  matrix[round - r][bj] = num++;
              for (int li = round - r - 1; li > r; li--)// left
                  matrix[li][r] = num++;
          }
          for (int j = 0; j < matrix.length; j++) {
            for (int i = 0; i < matrix[j].length; i++) {
              System.out.print(matrix[j][i]+" ");
            }
            System.out.println("");
          }
      }
    }将6楼的稍稍修改 更符合楼主要求 呵呵 和楼主一起学习了。