编程输出下列矩阵
例如:
输入:  5
输出:
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  

解决方案 »

  1.   

    其实用我们最初学得for循环就能做到,用for嵌套一个for就搞定了!!
      

  2.   

    这算是螺旋矩阵
    自己也有方法了 就行不怎么行
    什么用for循环什么的 就不要说了
    懂编程的都会知道
    问题是怎么控制
      

  3.   

    个人方法:
    int[] direct = {1, 0, -1, 0};初始定义
    int directX = 0, directY = 3;需要转向时
    directX = (directX + 1) % 4;
    directY = (directY + 1) % 4;
      

  4.   


    public class Matrix {
    private int[][] matrix;
    private int n; public Matrix(int n) {
    this.n = n;
    matrix = new int[n][n];
    int[] direct = new int[] { 1, 0, -1, 0 };
    int directX = 0, directY = 3, currentX = -1, currentY = 0;
    for (int count = 0; count < n * n;) {
    int nextX = currentX + direct[directX], nextY = currentY
    + direct[directY];
    if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0
    || matrix[nextY][nextX] != 0) {
    directX = (directX + 1) % 4;
    directY = (directY + 1) % 4;
    continue;
    }
    matrix[nextY][nextX] = ++count;
    currentX = nextX;
    currentY = nextY;
    }
    } public String toString() {
    StringBuffer sb = new StringBuffer();
    if (matrix != null) {
    int length = new String(n * n + "").length();
    String format = "%-" + length + "d ";
    for (int[] array : matrix) {
    for (int i : array) {
    sb.append(String.format(format, i));
    }
    sb.append('\n');
    }
    }
    sb.trimToSize();
    return sb.toString();
    }
    }
      

  5.   

    public static void main(String args[]){
    int constNum = 11;
    int n=1;
    //0 1 2 3 4 / 9 14 19 24 / 23 22 21 20 / 15 10 5 / 6 7 8 / 13 18 / 17 16 / 11 / 12
    int [] test = new int[constNum*constNum];

    int count = constNum;
    boolean hasSub = true;
    int []operator = new int[4];
    operator[0] = 1;
    operator[1] = constNum;
    operator[2] = -1;
    operator[3] = -constNum;
    int index = 0;
    int last = -1;
    while (count > 0) {
    for (int i = 0; i < count; i++) {
    test[last + operator[index]] = n++;
    last = last + operator[index]; }
    index = (index +1)%4;
    if(hasSub){
    count--;
    hasSub = false;
    }
    else{
    hasSub = true;
    }
    }
    for(int i=0;i<constNum*constNum;i++){
    if(i%constNum==0){
    System.out.println("");
    }
    System.out.print(test[i] + "\t");
    }
    }
      

  6.   


    public static void main(String args[]){
    int constNum = 11;
    int n=1;
    //0 1 2 3 4 / 9 14 19 24 / 23 22 21 20 / 15 10 5 / 6 7 8 / 13 18 / 17 16 / 11 / 12
    int [] test = new int[constNum*constNum];

    int count = constNum;
    boolean hasSub = true;
    int []operator = new int[4];
    operator[0] = 1;
    operator[1] = constNum;
    operator[2] = -1;
    operator[3] = -constNum;
    int index = 0;
    int last = -1;
    while (count > 0) {
    for (int i = 0; i < count; i++) {
    test[last + operator[index]] = n++;
    last = last + operator[index]; }
    index = (index +1)%4;
    if(hasSub){
    count--;
    hasSub = false;
    }
    else{
    hasSub = true;
    }
    }
    for(int i=0;i<constNum*constNum;i++){
    if(i%constNum==0){
    System.out.println("");
    }
    System.out.print(test[i] + "\t");
    }
    }
      

  7.   

    去wwww.chinajavaworld.com。你搜一下螺旋矩阵,里面有个很好的例子,讲解的和透彻,个人觉得很好。推荐。
      

  8.   


    public static int[][]  aa(int k){
    int[][] hui = new int[k][k];
    int row=0;
    int fx=0;
    for(int i=1;i<k*k;){
    if(fx%4==0){
    for(int j=row;j<k-row-1&&i<=k*k;j++){
    hui[row][j]=i;
    i++;
    }
    fx++;
    }else if(fx%4==1){
    for(int j=row;j<k-row-1&&i<=k*k;j++){
    hui[j][k-row-1]=i;
    i++;
    }
    fx++;
    }else if(fx%4==2){
    for(int j=row;j<k-row-1&&i<=k*k;j++){
    hui[k-1-row][k-j-1]=i;
    i++;
    }
    fx++;
    }else if(fx%4==3){
    for(int j=row;j<k-row-1&&i<=k*k;j++){
    hui[k-j-1][row]=i;
    i++;
    }
    fx++;
    row++;
    }
    }

    if(k%2==1){
    hui[(int)(k/2)][(int)(k/2)]=k*k;
    }else{
    hui[k/2][k/2-1]=k*k;
    }

    return hui;

      

  9.   

    这是我06年5月写的程序:
    http://blog.csdn.net/chouy/archive/2006/06/15/800188.aspx
      

  10.   

    从中心点开始往回走,每走一部数值减一。 其中走的路线也是有规律的,横的走几格,必定伴随着纵向的也走几格。ublic class FunClass { /**
     * @param args
     */
    public static void main(String[] args) {
    int arg = 5;  //设置你要得数

    int[][] arry = new int[arg][arg];
    int centerColumn = 0;
    int centerRow = 0;
    if(arg % 2 == 0){
    centerColumn = arg / 2 - 1;
    centerRow = arg / 2 ;
    } else {
    centerColumn = arg / 2;
    centerRow = arg / 2;
    }

    int value = arg * arg;
    arry[centerRow][centerColumn] = value--;

    boolean isMoveVertical = false;
    boolean isToLeft = false;
    boolean isUp = true;
    if(arg % 2 == 0){
    isToLeft = false;
    isUp = true;
    } else {
    isToLeft = true;
    isUp = false;
    }
    int currCol = centerColumn;
    int currRow = centerRow;
    for(int i = 1; i < arg; i++){
    for(int j = 0 ; j < 2; j++){
    for(int k = 0; k < i; k++){
    if(isMoveVertical){
    if(isUp){
    currRow--;
    } else {
    currRow++; 
    }
    } else {
    if(isToLeft){
    currCol--;
    } else {
    currCol++;
    }
    }
    arry[currRow][currCol] = value--;
    }
    isMoveVertical = !isMoveVertical;
    }
    isToLeft = !isToLeft;
    isUp = !isUp;
    }

    for(int i = 0; i < arg - 1; i++){
    arry[0][i] = i+1;
    }

    //output the array
    for(int i = 0; i < arg; i++){
    for(int j = 0; j < arg; j++){
    System.out.print(arry[i][j]);
    System.out.print(" ");
    }
    System.out.println();
    } }}