蛇形填数

本文介绍了一种在n*n方阵中按蛇形路径填充数字1至n*n的方法,并提供了完整的Java实现代码及运行结果示例。蛇形路径是指数字在矩阵中依次从右上到左下,再从左下到右上的方式排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:在n*n方阵里填入1,2,3,。。。,n*n,要求填成蛇形。例如n=4时方阵为

10 11 12 1
9  16 13 2
8  15 14 3
7  6  5  4


代码:

public class Demo06 {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("维数为10的蛇阵:");
		Matrix m1=new Matrix(10);
		m1.create();
		m1.print();
		
		System.out.println("维数为20的蛇阵:");
		Matrix m2=new Matrix(20);
		m2.create();
		m2.print();
		
	}

}

class Matrix{
	private int n;           //方阵维数
	private int matrix[][];  //方阵
	
	/*带参构造函数*/
	public Matrix(int n){
		this.n=n;
		this.matrix=new int[n][n];
		
		/*初始化为0*/
		for(int i=0;i<this.matrix.length;i++)
			for(int j=0;j<this.matrix[i].length;j++)
				this.matrix[i][j]=0;
	}
	
	/*构造蛇阵*/
	public void create(){
		int max=this.n*this.n;
		int num=1;
		int i=0;
		int j=this.n-1;
		this.matrix[i][j]=num;
		while(num<max){
			/*从上到下*/
			while(i+1<this.n&&(this.matrix[i+1][j]==0)){
				i++;
				num++;
				this.matrix[i][j]=num;			
			}
			
			/*从右到左*/
			while(j-1>=0&&this.matrix[i][j-1]==0){
				j--;
				num++;
				this.matrix[i][j]=num;
			}
			
			/*从下到上*/
			while(i-1>=0&&this.matrix[i-1][j]==0){
				i--;
				num++;
				this.matrix[i][j]=num;
			}
			
			/*从左到右*/
			while(j+1<n&&this.matrix[i][j+1]==0){
				j++;
				num++;
				this.matrix[i][j]=num;
			}
		}
	}
	
	/*输出蛇阵*/
	public void print(){
		for(int i=0;i<this.matrix.length;i++){
			for(int j=0;j<this.matrix[i].length;j++){
				System.out.printf("%-5d",this.matrix[i][j]);
			}
			System.out.println();
		}	
	}
}


运行结果:

维数为10的蛇阵:
28   29   30   31   32   33   34   35   36   1    
27   58   59   60   61   62   63   64   37   2    
26   57   80   81   82   83   84   65   38   3    
25   56   79   94   95   96   85   66   39   4    
24   55   78   93   100  97   86   67   40   5    
23   54   77   92   99   98   87   68   41   6    
22   53   76   91   90   89   88   69   42   7    
21   52   75   74   73   72   71   70   43   8    
20   51   50   49   48   47   46   45   44   9    
19   18   17   16   15   14   13   12   11   10   
维数为20的蛇阵:
58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   1    
57   128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  77   2    
56   127  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  145  78   3    
55   126  189  244  245  246  247  248  249  250  251  252  253  254  255  256  205  146  79   4    
54   125  188  243  290  291  292  293  294  295  296  297  298  299  300  257  206  147  80   5    
53   124  187  242  289  328  329  330  331  332  333  334  335  336  301  258  207  148  81   6    
52   123  186  241  288  327  358  359  360  361  362  363  364  337  302  259  208  149  82   7    
51   122  185  240  287  326  357  380  381  382  383  384  365  338  303  260  209  150  83   8    
50   121  184  239  286  325  356  379  394  395  396  385  366  339  304  261  210  151  84   9    
49   120  183  238  285  324  355  378  393  400  397  386  367  340  305  262  211  152  85   10   
48   119  182  237  284  323  354  377  392  399  398  387  368  341  306  263  212  153  86   11   
47   118  181  236  283  322  353  376  391  390  389  388  369  342  307  264  213  154  87   12   
46   117  180  235  282  321  352  375  374  373  372  371  370  343  308  265  214  155  88   13   
45   116  179  234  281  320  351  350  349  348  347  346  345  344  309  266  215  156  89   14   
44   115  178  233  280  319  318  317  316  315  314  313  312  311  310  267  216  157  90   15   
43   114  177  232  279  278  277  276  275  274  273  272  271  270  269  268  217  158  91   16   
42   113  176  231  230  229  228  227  226  225  224  223  222  221  220  219  218  159  92   17   
41   112  175  174  173  172  171  170  169  168  167  166  165  164  163  162  161  160  93   18   
40   111  110  109  108  107  106  105  104  103  102  101  100  99   98   97   96   95   94   19   
39   38   37   36   35   34   33   32   31   30   29   28   27   26   25   24   23   22   21   20   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值