A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小面团 初级黑马   /  2016-8-3 20:24  /  3139 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

5黑马币
写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。
         * 如: n = 4 则打印:
                1        2        3        4
                12        13        14        5
                11        16        15        6
                10        9        8        7
                ?

最佳答案

查看完整内容

[mw_shl_code=java,true] public static void main(String[] args) { int n = 4; //定义数组长 int[][] arr = new int[n][n]; //定义二维数组 int min = 1; //定义数组中最小的数 int count = 0; //定义圈数 fun(n,arr,min,count); for (int i = 0; i < n; i++) { //遍历数组并打印 System.out.println(Arrays.toString(arr)); } } /* * 定义方法: * 返回值类型 void * 参数列表(int n,int ...

4 个回复

倒序浏览
[Java] 纯文本查看 复制代码
	public static void main(String[] args) {
	
		int n = 4;					//定义数组长
		int[][] arr = new int[n][n];			//定义二维数组
		int min = 1;				//定义数组中最小的数
		int count = 0;				//定义圈数
		fun(n,arr,min,count);
		for (int i = 0; i < n; i++) {	//遍历数组并打印
			System.out.println(Arrays.toString(arr[i]));
		}
	}
	/*
	 * 定义方法:
	 * 返回值类型 void
	 * 参数列表(int n,int[][] arr,int min ,int count)
	 */
	public static void fun(int n,int[][] arr,int min ,int count) {
		for (int i = 0; i <= (n-1)*4 ; i++) {	//设置循环用来给数组赋值
			if(n == 0){							//如果组长为0则跳出循环
				break;
			}
			if(n == 1){							//如果组长为1则赋值并跳出循环
				arr[count][count] = min;
				break;
			}
			if(i < n-1){						//第一组,数组向右赋值   
				arr[count][count+i%(n-1)] = min++; 
			} else if (i < (n-1)*2){			//第二组,数组向下赋值     
				arr[count+i%(n-1)][count+n-1] = min++; 
			} else if (i < (n-1)*3) {			//第三组,数组向左赋值    
				arr[count+n-1][count+n-1-i%(n-1)] = min++; 
			} else if (i < (n-1)*4){			//第四组,素组向上赋值     
				arr[count+n-1-i%(n-1)][count] = min++;
			} else {      						//当一圈赋值完毕,圈数加一,边长减二,递归调用    
				fun(n-2,arr,min,count+1);
			}
		}
	}

回复 使用道具 举报
要求是二维数组,基础班学二维数组时还没学递归,所以我觉得这个代码更符合
import java.util.Scanner;

public class Demo3 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int m = sc.nextInt();
                int[][] arr = new int[m][m];
                int col=0, row = 0;
                arr[row][col] = 1;
                                                        //右                 //下                        //左                //上
                int[][] direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
                int directIndex = 0; //用来标识方向,一开始为右
               
                for(int i = 2; i<= m*m; i++){
                        //按照之前的方向,计算下一个坐标
                        row = row + direction[directIndex][0];
                        col = col + direction[directIndex][1];
                       
                        //如果计算所得的坐标超出了范围
                        if ( ( row >=m || row <0 )
                        ||(col >=m || col <0)
                        || (arr[row][col] != 0) ){
                            //复原
                                row = row - direction[directIndex][0];
                                col = col - direction[directIndex][1];
                                //换成下一个方向
                                directIndex++;
                                if (directIndex == 4) directIndex = 0;
                               
                                //计算新坐标
                                row = row + direction[directIndex][0];
                                col = col + direction[directIndex][1];
                        }
                       
                        arr[row][col] = i;
                }
               
                print(arr);
        }
       
        public static void print(int[][] arr){
                for(int i = 0; i<arr.length; i++){
                        for(int j = 0; j<arr[i].length; j++){
                                System.out.print(arr[i][j] + "\t");
                        }
                        System.out.println();
                }
        }
       

}
回复 使用道具 举报
[AppleScript] 纯文本查看 复制代码
public class Test {
	public static void main(String[] args) {
		System.out.println("请输入圈数:");
		Scanner sc = new Scanner(System.in);
		int quan = sc.nextInt();
		int[][] arr = new int[quan][quan];
		
		int fangxiang = 1;//1,2,3,4分别代表右,下,左,上
		//计数变量
		int num = 1;
		//行数
		int h = 0;
		//列数
		int l = 0;
		//开始输入
		while(num <= quan * quan) {
			//将num存入数组并加一
			arr[h][l] = num;
			num++;
			//判断下一步的方向并执行相应操作
			switch(fangxiang) {
			case 1:
				if(l + 1 < quan && arr[h][l + 1] == 0) {//索引不越界,并且在该方向上下一个值未被赋值
					l++;//沿方向增长
				}else {
					fangxiang = 2;//改变方向
					h++;//沿方向增长
				}
				break;
			case 2:
				if(h + 1 < quan && arr[h + 1][l] == 0) {
					h++;
				}else {
					fangxiang = 3;
					l--;
				}
				break;
			case 3:
				if(l - 1 >= 0 && arr[h][l - 1] == 0) {
					l--;
				}else {
					fangxiang = 4;
					h--;
				}
				break;
			case 4:
				if(h - 1 >= 0 && arr[h - 1][l] == 0) {
					h--;
				}else {
					fangxiang = 1;
					l++;
				}
				break;
				default:
			}
		}
		sc.close();
		//遍历输出数组
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + "\t");
			}
			System.out.println();
		}
	}
}

回复 使用道具 举报
好复杂的样子,一脸懵逼
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马