以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中


  1. import java.util.Scanner;  
  2.    
  3. /** 
  4.  * 从键盘输入一个整数(1~20) 
  5. 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。例如: 
  6. 输入数字2,则程序输出: 
  7. 1 2 
  8. 4 3 
  9. 输入数字3,则程序输出: 
  10. 1 2 3 
  11. 8 9 4 
  12. 7 6 5 
  13. 输入数字4, 则程序输出: 
  14. 1  2   3   4 
  15. 12  13  14  5 
  16. 11  16  15  6 
  17. 10   9  8   7 
  18.  *  
  19.  * @author wangxu 
  20.  * date:2012/3/24 
  21.  * 说明:时间有限,给一个不太优化的代码,足以实现功能 
  22.  * 思路:先声明一个二维数组用来存储每个对应的数字,对数组赋值完成之后就可以打印输出 
  23.  * 我的代码可以实现整型以内的所有合法数字,并不局限于20以内 
  24.  *  
  25.  */  
  26. public class Demo {  
  27. static int arr[][];//二维数组用来存储数字  
  28. static int currentNumber = 1;// 从1开始打印,用来控制整个流程需要打印的数字  
  29. static int num = 0;//用来判断何时可以打印  
  30. public static void main(String args[]) throws Exception {  
  31. Scanner in = new Scanner(System.in);//获取输入流  
  32. System.out.println("请输入您希望打印的矩阵的最大的值:");//打印提示信息  
  33. String input = in.next();//获取用户的输入  
  34. int count = 0;//计数器  
  35. if (input.matches("\\d+")) {// 输入正确,用来检验用户输入的是否是数字  
  36. num = Integer.parseInt(input);  
  37. count = num;// count用来控制在每一个方向上打印几个数字,第一次打印时候和用户输入的数字相同  
  38. else {  
  39. System.out.println("您输入的数据不正确,系统推出!");  
  40. System.exit(0);  
  41. }  
  42. arr = new int[num][num];//实例化数组  
  43. moveRight(count, 00);//调用右移函数  
  44. }  
  45.    
  46. public static void moveRight(int count, int rowIndex,  
  47. int columnIndex) {//count表示移动几步,由题目可以看出右移时候次数和是用户输入的数字,下移减1,左移不变,上移减1,右移不变  
  48. if(judge()){//如果数组已经赋值结束,就打印出来  
  49. print();  
  50. }else{  
  51. int temp = 0;//临时变量  
  52. while (temp < count) {  
  53. arr[rowIndex][columnIndex] = currentNumber;  
  54. temp ++;//临时变量自增  
  55. columnIndex++;//向右移动  
  56. currentNumber++;//当前要打印的数字自增  
  57. }  
  58. count--;//将打印次数减1  
  59. columnIndex--;//下标归位  
  60. rowIndex++;//下移一位  
  61. moveDown(count,rowIndex,columnIndex);  
  62. }  
  63. }  
  64.    
  65. public static void moveDown(int count,int rowIndex,int columnIndex) {  
  66. if(judge()){  
  67. print();  
  68. }else{  
  69. int temp = 0;  
  70. while(temp < count){  
  71. arr[rowIndex][columnIndex] = currentNumber;  
  72. temp ++;  
  73. rowIndex++;  
  74. currentNumber++;  
  75. }  
  76. rowIndex--;  
  77. columnIndex--;//左移一位  
  78. moveLeft(count, rowIndex, columnIndex);  
  79. }  
  80.    
  81. }  
  82.    
  83. public static void moveLeft(int count,int rowIndex,int columnIndex) {  
  84. if(judge()){  
  85. print();  
  86. }else{  
  87. int temp = 0;//临时变量  
  88. while(temp < count){  
  89. arr[rowIndex][columnIndex]=currentNumber;  
  90. temp ++;  
  91. columnIndex--;//左移一位  
  92. currentNumber++;//数字自增  
  93. }  
  94. count--;//次数减1  
  95. columnIndex++;  
  96. rowIndex--;//上移一位  
  97. moveUp(count, rowIndex, columnIndex);  
  98.    
  99. }  
  100. }  
  101.    
  102. public static void moveUp(int count,int rowIndex,int columnIndex) {  
  103. if(judge()){  
  104. print();  
  105. }else{  
  106.    
  107. int temp = 0;  
  108. while(temp < count){  
  109. arr[rowIndex][columnIndex] = currentNumber;  
  110. temp ++;  
  111. rowIndex--;  
  112. currentNumber++;  
  113. }  
  114. rowIndex++;  
  115. columnIndex++;//右移一位  
  116. moveRight(count, rowIndex, columnIndex);  
  117. }  
  118.    
  119. }  
  120. public static void print(){//在控制台打印  
  121. for(int row[]:arr){  
  122. for(int column:row){  
  123. System.out.print(column+"\t");  
  124. }  
  125. System.out.println();//换行打印  
  126. }  
  127. }  
  128. public static boolean judge(){  
  129. if(currentNumber>Math.pow(num, 2)){//如果数组已经赋值结束,就打印出来  
  130. return true;  
  131. }else{  
  132. return false;  
  133. }  
  134. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值