<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);
}
}