数组-13. 螺旋方阵(20)

<p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 14.399999618530273px; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">所谓“螺旋方阵”,是指对任意给定的N,将1到N*N的数字从左上角第1个格子开始,按顺时针螺旋方向顺序填入NxN的方阵里。本题要求构造这样的螺旋方阵。</p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 14.399999618530273px; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);"><strong>输入格式:</strong></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 14.399999618530273px; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">输入在一行中给出一个正整数N(<10)。</p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 14.399999618530273px; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);"><strong>输出格式:</strong></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 14.399999618530273px; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">输出NxN的螺旋方阵。每行N个数字,每个数字占3位。</p><span style="color: rgb(51, 51, 51); font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 14.399999618530273px; background-color: rgb(250, 250, 250);">输入样例:</span><pre style="margin-top: 1.5em; margin-bottom: 1.5em; padding: 0px; border: 0px; font-family: 'Droid Sans Mono', Consolas, 'Courier New', monospace; line-height: 14.399999618530273px; vertical-align: baseline; overflow: auto; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">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

 
本题采用递归!!!!



import java.util.Scanner;

public class Main {
	
	public static void main(String []args){
		int N,i,j;
		Scanner scanner=new Scanner(System.in);
		N=scanner.nextInt();
		int [][]matrix=new int[N][N];
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				matrix[i][j]=0;
			}
		
		}
		
		
		fillMatrix(matrix,0,N-1,0,N-1,1);
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				System.out.printf("%3d",matrix[i][j]);
			}
			System.out.printf("\n");
		}
	}
	
	public static void fillMatrix(int [][]matrix,int left,int right,int top,int bottom,int start){
		int i,j;
		if(left>right||top>bottom)return;
		
		for(i=left;i<=right;i++){
			matrix[top][i]=start++;
		}
		
		for(i=top+1;i<=bottom;i++){
			matrix[i][right]=start++;
		}
		
		for(i=right-1;i>=left;i--){
			matrix[bottom][i]=start++;
		}
		for(i=bottom-1;i>=top+1;i--){
			matrix[i][left]=start++;
		}
		
		fillMatrix(matrix,left+1,right-1,top+1,bottom-1,start);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值