zoukankan      html  css  js  c++  java
  • 螺旋数数组

    import java.util.Scanner;
    public class HellowWorld {
        public static void main(String[] argv ) {
            int hang,lie; //用户输入的行列数组
            int total=0;//数组中总共的元素个数
            int[][]decation= {{0,1},{1,0},{0,-1},{-1,0}}; //定义一个方向数组,这是方便转向的 右,下,左,上
            int decationIndex=0;//方向数组的下标 初始化为向右
            int row=0; //诶个填充的时候的行
            int col=0;//诶个填充的时候的列
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入行数");
            hang=sc.nextInt();
            System.out.println("请输入列数");
            lie=sc.nextInt();
            total=hang*lie;
            int[][] arr=new int[hang][lie]; //申明二维数组
            arr[row][col]=1;//第一行第一列为1
    /*        填充数组                                  */
            for(int i=2;i<=total;i++)
            {
                //确认下一个存放的数组位置
                row=row+decation[decationIndex][0]; 
                col=col+decation[decationIndex][1];
                //处理下转向的问题 行是到row=lie-1的时候转,列是到hang-1的时候转,新赋值的行和列不能有值
                if((row>=hang)||(row<0)||(col>=lie)||(col<0)||arr[row][col]!=0)
                {
                    //回到未出界之前
                    row=row-decation[decationIndex][0]; 
                    col=col-decation[decationIndex][1];
                    decationIndex++;//转向
                    if(decationIndex==4)
                    {
                        decationIndex=0;
                    }
                    //新的坐标
                    row=row+decation[decationIndex][0]; 
                    col=col+decation[decationIndex][1];
                    
                }
                
                //将数值放进去
                arr[row][col]=i;
            }//end for
            for(int i=0;i<arr.length;i++)
            {
                for(int j=0;j<arr[i].length;j++)
                System.out.print(arr[i][j]+" ");
                System.out.println();
            }
    
    
        }//endmain,
    }//endclass

     方法二:

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int m=sc.nextInt();
            int n=sc.nextInt();
            int[][] arr=new int [m][n]; //二维数组
            arr[0][0]=1;
            int cir=0;//有几个圈
            int row=0;
            int col=0;
            for(int i=2;i<=m*n;i++)
            {
                if(row==cir&&col<n-1-cir) //向右
                {
                    col++;
                }
                else if(col==n-1-cir&&row<m-1-cir)//向下
                    row++;
                else if(row==m-1-cir&&col>cir)//向左
                    col--;
                else if(col==cir&&row>cir+1)//向上 完成一圈
                    row--;
                else {
                    col++;cir++;
                }
                arr[row][col]=i;
            }
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print(arr[i][j]+ " ");
                }
                System.out.println();
            }
    
    
        }
  • 相关阅读:
    Windows7共享设置
    13-运算符
    13-数据类型转换
    06-移动web之flex布局
    09-sass
    08-less预处理器
    移动端必须掌握知识点
    11-JS变量
    10-响应式
    07-rem
  • 原文地址:https://www.cnblogs.com/xuwangqi/p/11045047.html
Copyright © 2011-2022 走看看