黑马程序员技术交流社区

标题: =========!!!!!!===敢挑战吗?===!!!!!!======= [打印本页]

作者: 黄玉昆    时间: 2013-4-30 08:33
标题: =========!!!!!!===敢挑战吗?===!!!!!!=======
本帖最后由 黄玉昆 于 2013-5-2 15:40 编辑

下面的一道题是我给刘胜寒出的一道题,大家也可以参与做一做啊,有惊喜哦
这道题是给敢于挑战,敢于挖掘自己潜力的人出的,如果谁觉得能在规定时间做出来,敢于挑战我的题目,那么你就是厉害的,我就是佩服你的。
如果你觉得自己还不敢挑战,没关系,以后还有机会,不敢挑战的或者认为自己还差点的,那么就不要回复了。
另外,我会根据回复的时间,来评分的,回复越快,答案越清晰,分数当然越高了。


如果来挑战了,大家直接在本楼层编辑即可,不会编辑的请看帖:http://bbs.itheima.com/thread-42978-1-1.html,可以看其中的沙发层的“编辑贴”说明

规定时间:48小时,即从2013.4.30-5.2的9:00

注:如果你是真正的黑马,敢挑战,就要完完全全自己做出来,如果是从网上当的,那么算了吧,我鄙视这样的人,这样的人给黑马丢人,更丢自己的人。
不过我相信大家都是好样的,有能力自己做出来。

话不多说,看题(回复可见哦):


我把相对比较好的传上来,大家可以看一看啊(排名不分先后)
曹睿翔.zip (1.19 KB, 下载次数: 61)
蓝色骨头.zip (1.48 KB, 下载次数: 40)
丘凤光.zip (1.08 KB, 下载次数: 44)
李罡.zip (1.4 KB, 下载次数: 39)


作者: 丘凤光    时间: 2013-4-30 08:38
本帖最后由 丘凤光 于 2013-4-30 11:53 编辑

昆哥又有啥好活动了?

真是不容易的题啊  

绝对自己写

MyPrint.zip

1.08 KB, 阅读权限: 100, 下载次数: 1

代码


作者: 孙胜    时间: 2013-4-30 08:41
看看,这是个什么样的题目
作者: 付银攀    时间: 2013-4-30 08:54
恩,虽然不确定会不会做,但是挑战是必须敢的
作者: 刘胜寒    时间: 2013-4-30 08:55
我来瞅瞅   
作者: 黄基焜    时间: 2013-4-30 08:59
我也来看看是什么题目

作者: 杜超    时间: 2013-4-30 09:03
试试看看
作者: 燕国庆    时间: 2013-4-30 09:08
前来打擂!!
作者: hyw840705183    时间: 2013-4-30 09:09
本帖最后由 hyw840705183 于 2013-5-1 14:19 编辑

我看下什么题目

以下是我的代码:
  1. class Test
  2. {
  3. private String[][] a=new String[0][0];//定义一个字符串数组
  4.         private int x=0,y=0;//初始化数组行与列
  5.         private int number=1;//第一个数字
  6.         private String string;//数字转换的字符串

  7.         public Test(int length)
  8.         {
  9.                 a=new String[length+2][length+2];//由于外围有*号,故给数组分配空间要多2个
  10.                 x=(length+1)/2;//确定第一个数字1的行位置
  11.                 y=(length+1)/2;//确定第一个数字1的列位置

  12.                 /*下面的for循环是初始化外围的*号*/
  13.                 for(int l=0;l<length+2;l++)
  14.                 {
  15.                         for(int k=0;k<length+2;k++)
  16.                         {
  17.                                 if(l==0||l==length+1)
  18.                                 {
  19.                                         a[l][k]="*";//如果行号为第一行和最后一行,则为其赋值为*
  20.                                 }
  21.                                 else
  22.                                 {
  23.                                         if(k==0||k==length+1)
  24.                                         {
  25.                                                 a[l][k]="*";//如果不是的话,则判断是不是第一列和最后一列,是则为其赋值*
  26.                                         }
  27.                                         else
  28.                                                 continue;//否则跳出本次循环
  29.                                 }
  30.                         }
  31.                 }
  32.                 a[x][y]=toString(1);//把1转换为字符串赋值给数组
  33.                 toRight();//第一次则往右边开始自增
  34.         }

  35.         /*数字往上面增加*/
  36.         private void toUp()
  37.         {
  38.                 if(a[x-1][y]==null)
  39.                 {
  40.                         assignment(--x, y);//x-1行y列上没有值,则赋值
  41.                         if(a[x][y+1]==null)
  42.                         {
  43.                                 toRight();//赋值后的位置的上面为空则转向往右自增数字
  44.                         }
  45.                         else
  46.                                 toUp();//否则继续向上自增赋值
  47.                 }
  48.         }

  49.         /*数字往下面增加*/
  50.         private void toDown()
  51.         {
  52.                 if(a[x+1][y]==null)
  53.                 {
  54.                         assignment(++x, y);//x+1行y列上没有值,则赋值
  55.                         if(a[x][y-1]==null)
  56.                         {
  57.                                 toLeft();//赋值后的位置的上面为空则转向往左自增数字
  58.                         }
  59.                         else
  60.                                 toDown();//否则继续向下自增赋值
  61.                 }
  62.         }

  63.         /*数字往左边增加*/
  64.         private void toLeft()
  65.         {
  66.                 if(a[x][y-1]==null)
  67.                 {
  68.                         assignment(x, --y);//x行y-1列上没有值,则赋值
  69.                         if(a[x-1][y]==null)
  70.                         {
  71.                                 toUp();//赋值后的位置的上面为空则转向往上自增数字
  72.                         }
  73.                         else
  74.                                 toLeft();//否则继续向左自增赋值
  75.                 }
  76.         }   
  77.         
  78.         /*数字往右边增加*/
  79.         private void toRight()
  80.         {
  81.                 if(a[x][y+1]==null)
  82.                 {
  83.                         assignment(x, ++y);//x行y+1列上没有值,则赋值
  84.                         if(a[x+1][y]==null)
  85.                         {
  86.                                 toDown();//赋值后的位置的上面为空则转向往下自增数字
  87.                         }
  88.                         else{
  89.                                 toRight();//否则继续向右自增赋值
  90.                         }
  91.                 }
  92.         }  
  93.         
  94.         /*给数组赋值*/
  95.         private void assignment(int x,int y)
  96.         {
  97.                 number++;//数字自增
  98.                 string=toString(number);//将数字转换为字符串
  99.                 a[x][y]=string;//将字符串赋值给数组
  100.         }

  101.         /*将数字转换为字符串,返回字符串*/
  102.         private String toString(int num)
  103.         {
  104.                 return String.valueOf(num);
  105.         }
  106.         
  107.         /*打印出所有的数字和*号*/
  108.         public void print()
  109.         {
  110.                 for(int i=0;i<a.length;i++)
  111.                 {
  112.                         for(int j=0;j<a.length;j++)
  113.                         {
  114.                                 System.out.print(a[i][j]+"\t");//输出的值用制表符\t空开
  115.                         }
  116.                 System.out.println();
  117.                 }
  118.         }
  119. }

  120. /*主类*/
  121. public class TestMain
  122. {
  123.         public static void main(String[] args)//主函数
  124.         {
  125.     java.util.Scanner in = new java.util.Scanner(System.in);//创建输入对象
  126.     System.out.println("Please input a >0's integer number,presss 0 exit...");
  127.     int number = in.nextInt();//用number接收输入的值
  128.     while(number != 0)//设置输入循环,输入0退出
  129.     {
  130.      if(number < 0)
  131.       System.out.println(number+" is invaild!");//输入小于0则提示错误
  132.      else
  133.       new Test(number).print();//创建对象调用print()
  134.      System.out.println("\nPlease input a >0's integer number,presss 0 exit...");
  135.      number = in.nextInt();//循环输入
  136.     }
  137.         }
  138. }
复制代码
这下可以了,改了下主类,也可以循环输入。。。
运行结果如图

运行结果.PNG (2.88 KB, 下载次数: 0)

n=5的运行结果

n=5的运行结果

作者: 石琪    时间: 2013-4-30 09:28
看看是啥。
作者: 黑马伍哲沂    时间: 2013-4-30 09:45
本帖最后由 黑马伍哲沂 于 2013-4-30 20:44 编辑

有点思路,把这矩阵分成4部分。把值分别存入二维数组。打印的时候,*号用强转。
但我觉得我做不下去了。。来宣布放弃的了。。。。  

如下图,真心不敢往下找规律了,头疼= =。

123.jpg (28.74 KB, 下载次数: 0)

123.jpg

作者: 李德全    时间: 2013-4-30 09:55
本帖最后由 李德全 于 2013-4-30 13:46 编辑

看看啥题

做出来啦,嘻嘻
  1. /*
  2. 需求:顺时针螺旋数字

  3. 说明:        老师,我刚学了7天毕老师基础视频,
  4.                 不知道怎么获取输入数字,
  5.                 就用主函数main中的String[] args传递。
  6.                 老师您验证的时候请麻烦传递一下。*_*

  7. 思路:
  8. 1.螺旋为顺时针,且以4为周期重复。
  9. 2.螺旋规律为:
  10. 循环次数        1        2        ...        

  11.         Y加                1        3        .
  12.         X加                1        3        .
  13.         Y减                2        4        .
  14.         X减                2        4


  15. */


  16. class ShuZhuDemo
  17. {
  18.         public static void main(String[] args)
  19.         {
  20.                 int z = Integer.parseInt(args[0]);        //获取输入数字
  21.                 System.out.println("输入的数字为:"+z);
  22.                
  23.                 int[][] arr = new int[z+2][z+2];        //创建数组
  24.                 int mid;                                                        //数字1的位置坐标
  25.                 if(z%2==0)                //偶数
  26.                         mid = z/2;
  27.                 else                        //奇数
  28.                         mid = (z+1)/2;

  29.                 /*开始计算*/

  30.                 int up = 1;        //X,Y增减的值。规律为:1,1,2,2,3,3,4,4,....
  31.                 int con=1;        //螺旋位置计数 好比一长方形四条边的位置,1,2,3,4分别代表上边,右边,下边,左边
  32.                 for(int x=mid, y=mid, m=1; m<=z*z ; m++ )
  33.                 {
  34.                         arr[x][y] = m;
  35.                         switch(con)
  36.                         {
  37.                                 case 1:
  38.                                         if(y<mid+up)
  39.                                                 y++;
  40.                                         else
  41.                                         {                                                       
  42.                                                 con++;
  43.                                                 x++;
  44.                                         }
  45.                                         break;
  46.                                 case 2:
  47.                                         if(x<mid+up)
  48.                                                 x++;
  49.                                         else
  50.                                         {
  51.                                                 y--;
  52.                                                 con++;                                                       
  53.                                         }
  54.                                         break;
  55.                                 case 3:
  56.                                         if(y>mid-up)
  57.                                                 y--;
  58.                                         else
  59.                                         {
  60.                                                 x--;
  61.                                                 con++;
  62.                                         }
  63.                                         break;
  64.                                 case 4:
  65.                                         if(x>mid-up)
  66.                                                 x--;
  67.                                         else
  68.                                         {                                                       
  69.                                                 con = 1;
  70.                                                 y++;
  71.                                                 up++;
  72.                                         }
  73.                                         break;
  74.                                 default: break;
  75.                                
  76.                         }
  77.                 }
  78.                 System.out.println("经过计算得:");
  79.                 System.out.println();
  80.                 for(int x=0; x<z+2; x++)                //打印数组
  81.                 {
  82.                         for(int y=0; y<z+2; y++)
  83.                                 if(arr[x][y]==0)
  84.                                         System.out.print("*\t");        //数组0元素替换为*
  85.                                 else
  86.                                         System.out.print(arr[x][y]+"\t");
  87.                         System.out.println();
  88.                         System.out.println();
  89.                 }
  90.         }
  91. }
复制代码
请老师指导!
作者: 刘沛霞    时间: 2013-4-30 09:57
我挑战下看看
作者: 王亚东    时间: 2013-4-30 10:02
本帖最后由 王亚东 于 2013-4-30 16:15 编辑

  1. public class Demo01 {

  2. public static void print(int[][] nums,int n,int lv2,int lv1){
  3. if(n == 2){
  4. nums[lv1][lv1] = 1;
  5. nums[lv1][lv1+1] = 2;
  6. nums[lv1+1][lv1] = 4;
  7. nums[lv1+1][lv1+1]= 3;
  8. fillIn(nums);
  9. for(int[] line : nums){
  10. for(int i: line){
  11. if(i == -1){
  12. System.out.print("* ");
  13. continue;
  14. }
  15. System.out.print(i+" ");
  16. if(i<10)
  17. System.out.print(" ");
  18. }
  19. System.out.println();
  20. }
  21. return;
  22. }
  23. if(n%2==0){
  24. for(int i = 0;i<n;i++){
  25. nums[n-1+lv2][i+lv2] = n*n-i;
  26. }
  27. for(int i = 0;i<n-1;i++){
  28. nums[i+lv2][n-1+lv2] = n*n-(n-1)*2+i;
  29. }
  30. print(nums,n-1,lv2,lv1);
  31. }else{
  32. for(int i = 0;i<n;i++){
  33. nums[lv1][lv1+i] = n*n-(n-1)+i;
  34. }
  35. for(int i = 0;i<n-1;i++){
  36. nums[(n-1-i)+lv1][lv1] = n*n-(n-1)*2+i;
  37. }
  38. print(nums,n-1,lv2+1,lv1+1);
  39. }
  40. }

  41. private static void fillIn(int[][] nums){
  42. int n = nums.length;
  43. for(int i = 0;i<n-1;i++){
  44. nums[0][i] = -1;
  45. }
  46. for(int i = 1;i<n-1;i++){
  47. nums[i][0] = -1;
  48. }
  49. for(int i = 0;i<n-1;i++){
  50. nums[n-1][i] = -1;
  51. }
  52. for(int i = 0;i<n;i++){
  53. nums[i][n-1] = -1;
  54. }
  55. }

  56. public static void main(String[] args) throws Exception {
  57. int n = 4;
  58. int[][] nums = new int[n+2][n+2];
  59. print(nums,n,1,1);
  60. }
  61. }
复制代码

作者: 殇_心。    时间: 2013-4-30 10:08
路过```每回都是提醒都是需要10个字符。
作者: 傻瓜一点红    时间: 2013-4-30 10:18
{:soso_e114:}
作者: 高新星    时间: 2013-4-30 10:23

作者: 刘胜寒    时间: 2013-4-30 10:24
你丫的 图片 是用excel 做的吧
作者: 黄玉昆    时间: 2013-4-30 10:27
刘胜寒 发表于 2013-4-30 10:24
你丫的 图片 是用excel 做的吧

嘿嘿,当然了,要不手工多费事啊,;P
作者: 陈圳    时间: 2013-4-30 10:40
本帖最后由 陈圳 于 2013-4-30 12:55 编辑
  1. <p>
  2. import java.util.Arrays;
  3. public class ArraysTest {</p><p>    /**螺旋矩阵数组.
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.      retangleArrays(5);
  8.     }
  9.     public static void retangleArrays(int num){
  10.      int index;//取得数组最中间的值
  11.      String[][] temp=new String[num+2][num+2];
  12.      int[][] arr=new int[num][num];
  13.             if(num%2==0){//取开始时的坐标位置
  14.                 index=(num>>1)-1;
  15.                 arr=getArray2(num,index);
  16.             }
  17.             else {
  18.              index=num>>1;
  19.              arr=getArray1(num,index);
  20.             }
  21.         for(int i=0;i<num+2;Arrays.fill(temp[i++],"*"));//进行外圈赋'*';
  22.         for(int i=0;i<arr.length;i++){
  23.          for(int j=0;j<arr.length;j++)
  24.           temp[i+1][j+1]=""+arr[i][j];//进行赋值操作
  25.         }
  26.         for(int i=0;i<temp.length;i++)//这里输出格式不好,但是最简单
  27.          System.out.println(Arrays.toString(temp[i]));
  28.     }
  29.     public static int[][] getArray2(int num,int index){//这个是偶数
  30.       int[][] arr=new int[num][num];
  31.       int[][] temp=getArray1(num-1,index);//这里调用第一个奇数方法,得到奇数螺旋的数组.然后在外围加上一圈就行了
  32.       for(int i=0;i<temp.length;i++)//这里赋值,但总感觉有更好的方式完成这步...
  33.        for(int j=0;j<temp[i].length;j++){
  34.         arr[i][j]=temp[i][j];
  35.        }
  36.       int count=num*num,length=num*2-1;//这里等于4--16;
  37.       for(int i=0;i<=length;i++){//赋上最外层一圈
  38.        if(i<num)
  39.         arr[num-1][i]=count--;
  40.        else
  41.         arr[length-i][num-1]=count--;
  42.       }
  43.          return arr;
  44.     }
  45.     public static int[][] getArray1(int num,int index){//这个是奇数螺旋
  46.        int arr[][]=new int[num][num];
  47.           int rows=index;//二维的横坐标
  48.           int cols=index;//二维的纵坐标
  49.           int fixRow=index-1;//内层循环的值最小
  50.           int fixCol=index+1;//内层循环的值最小
  51.           arr[rows][cols]=1;
  52.           for(int i=2;i<=arr.length*arr.length;){
  53.                   if(cols==fixRow&&rows==fixRow){//当走到小边缘的时候,就扩大其运转范围
  54.                           if(fixRow!=0){
  55.                                   fixRow--;
  56.                                   fixCol++;
  57.                           }
  58.                   }
  59.                   if(cols<=fixCol&&rows<=fixCol){
  60.                           if(cols<fixCol){
  61.                                   cols++;
  62.                                   arr[rows][cols]=i;
  63.                                   i++;
  64.                           }else{
  65.                                   rows++;
  66.                                   arr[rows][cols]=i;
  67.                                   i++;
  68.                           }
  69.                   }
  70.                   if(cols==fixCol&&rows==fixCol){//当走到最大边缘的时候
  71.                           for(int j=0;j<fixCol*2+1;j++){
  72.                                   if(cols>fixRow){
  73.                                           cols--;
  74.                                           arr[rows][cols] = i;
  75.                                           i++;
  76.                                   }        else if(rows>fixRow){
  77.                                                   rows--;
  78.                                                   arr[rows][cols] = i;
  79.                                                   i++;
  80.                                           }
  81.                           }
  82.                   }
  83.           }
  84.           return arr;
  85.     }
  86. }
  87. </p>
  88. 怎么设置仅作者可见....???
复制代码

作者: lipingan0520    时间: 2013-4-30 11:04
学习学习:)
作者: HM汪磊    时间: 2013-4-30 11:14
额????????????来看看
作者: 郭军亮    时间: 2013-4-30 11:34
先来看看吧
作者: Sword    时间: 2013-4-30 11:34
先看看再说吧
作者: 何旭程    时间: 2013-4-30 11:34
看看题目~
作者: 932759732    时间: 2013-4-30 11:55
先看看题
作者: 刘茂林    时间: 2013-4-30 12:00
先看下题把。
作者: 种生祥    时间: 2013-4-30 12:20
{:soso_e132:}
作者: 尹丽峰    时间: 2013-4-30 12:24
什么东东?
作者: 袁梦希    时间: 2013-4-30 12:29
其实早看到这帖子了,只是一直犹豫回不回,回了就必须做题,但是还没空思考。我还是先拿题以后有时间再做吧   {:soso_e102:}
作者: 蔡汉康    时间: 2013-4-30 12:40
本帖最后由 蔡汉康 于 2013-5-1 16:36 编辑

提交作业了~!!!!!!!
不知道为什么只能到5x5的矩阵
6x6的矩阵就不行了!!!

WWW.png (11.4 KB, 下载次数: 0)

WWW.png

蔡汉康-----LuoXuanArray2.rar

1.02 KB, 阅读权限: 100, 下载次数: 1


作者: 刘胜寒    时间: 2013-4-30 12:58
我在考虑要不要贴代码......

作者: 曹睿翔    时间: 2013-4-30 13:13
本帖最后由 曹睿翔 于 2013-4-30 22:22 编辑

我来晚了啊,玉圣!时间不对吧,48小时not equal to今天十点到明天吧9点吧,我还在店里,回宿舍看看
我的网挂了,上传附件打不开。我去年买了个表联通
  1. /*貌似开始学java也没打过这么多次的二维数组
  2. 说说思路。
  3. 1、先得到由内向外的螺旋状的二维数组
  4.   1)迭代方法获取。int[3][3]由int[2][2]的差别在于多了一维(也就是两个边)。用循环控制加上这两个边
  5.   2)加上两个边后,是空心的,这时就需要双重for循环把int[2][2]填进int[3][3]中
  6. 2、打印就行了*/
  7. public class Test {

  8.         /**
  9.          * @param args
  10.          */
  11.         public static void main(String[] args) {
  12.                 // TODO Auto-generated method stub
  13.                 //获取二维数组array
  14.                 int[][] array = getArray(7);
  15.                 /*for(int[] arr:array){
  16.                         for(int a:arr){
  17.                                 System.out.println(a);
  18.                         }
  19.                 }*/
  20.                 //添加*后打印
  21.                 addXX(array);
  22.                
  23.                
  24.         }
  25.         
  26.         public static void addXX(int[][] array){
  27.                 //需要注意的就一点,个位数字占的空间小,想对齐加tab
  28.                 int len = array.length;
  29.                 for(int x= 0;x<=len+1;x++){
  30.                         System.out.print("*\t");
  31.                 }
  32.                 System.out.println();
  33.                 for(int x = 0;x<len;x++){
  34.                         System.out.print("*\t");
  35.                         for(int y = 0 ;y<len;y++){
  36.                                 System.out.print(""+array[x][y]+"\t");
  37.                         }
  38.                         System.out.println("*\t");
  39.                                 
  40.                         }
  41.                
  42.                 for(int x= 0;x<=len+1;x++){
  43.                         System.out.print("*\t");
  44.                 }
  45.         }
  46.         
  47.         public static int[][] getArray(int n){
  48.                 int[][] newArr = null;
  49.                 int[][] arr = null;
  50.                 if(n==1){
  51.                          newArr = new int[][]{{1}};
  52.                          return newArr;
  53.                 }else if(n==2){
  54.                          newArr= new int[][]{{1,2},{4,3}};
  55.                          return newArr;
  56.                 }else if(n>2){
  57.                          newArr = new int[n][n];
  58.                          int a = n-1;
  59.                          arr = getArray(a);
  60.                          //当n>2时就需要考虑 ,把维度小的数组加进维度大的中去,分两种情况,一种是偶维度插进奇伟渡,另外一种相反
  61.                          if(n%2==0){
  62.                                  for(int x = 0; x<n-1;x++){
  63.                                          for(int y =0;y<n-1;y++){
  64.                                                  newArr[x][y] =arr[x][y];
  65.                                          }
  66.                                  }
  67.                          }else{
  68.                                  for(int x = 0; x<n-1;x++){
  69.                                          for(int y = 0;y<n-1;y++){
  70.                                                  newArr[x+1][y+1] = arr[x][y];
  71.                                          }
  72.                                  }
  73.                                  
  74.                          }
  75.                          //给高一维的数据加边
  76.                          if(n%2==0){
  77.                                     int x = arr[0][n-2];
  78.                                         for(int b = 0;b <n;b++){
  79.                                                 System.out.println(x);
  80.                                                 newArr[b][n-1] = ++x;
  81.                                                 System.out.println(newArr[b][n-1]);
  82.                                         }
  83.                                         int y =newArr[n-1][n-1];
  84.                                         for(int c = 0; c<n-1; c++){
  85.                                                 newArr[n-1][n-2-c] = ++y;
  86.                                         }               
  87.                                 }else{
  88.                                         //提升x作用域
  89.                                         int x = arr[n-2][0];
  90.                                         for(int b = 0;b<n;b++){
  91.                                                 //int x = arr[n-2][0];这句不能放在里边,不然x在循环过程中一直不变
  92.                                                 //System.out.println(x);
  93.                                                 newArr[n-b-1][0] = ++x;
  94.                                                 //System.out.println(newArr[0][0]);
  95.                                         }
  96.                                         int y = newArr[0][0];//与上句类似
  97.                                         for(int c =0;c<n-1;c++){                                                
  98.                                                 newArr[0][c+1] = ++y;               
  99.                                         }
  100.                                        
  101.                                 }
  102.                          return newArr;
  103.                         
  104.                 }else{
  105.                         System.out.println("输入错误");
  106.                 }
  107.                
  108.                 //n=3,arr[1][]
  109.                
  110.                 return newArr;
  111.                
  112.         }
  113.          

  114. }
复制代码

作者: 刘胜寒    时间: 2013-4-30 13:20
上图不解释 .....

F2}DN~5T16Z67TLM_`VXNZU.jpg (16.08 KB, 下载次数: 0)

F2}DN~5T16Z67TLM_`VXNZU.jpg

作者: 刘胜寒    时间: 2013-4-30 13:33
  1. class  inVrileMatrix
  2. {
  3.          private int n;
  4.          inVrileMatrix(int n)
  5.          {
  6.                  this.n = n;
  7.                  showMatrix();
  8.          }
  9.          public void showMatrix()
  10.          {
  11.                  if(n%2==0)
  12.                          show1(n);
  13.                  else show2(n);
  14.          }
  15.          /*
  16.           * show1 和show2 方法思路基本一致  每次确定是个坐标 下面的 Up*  Low*  Right*  Left*  是 上下左右四个点   这四个点 一用就两个值
  17.           * Up* 是左上  Low 是右下  Right 是右下  Left 是左下
  18.           * 所以可以用两个值去替换这八个变量  不想去 去替换了  myeclipse 提供了替换工具 没空了
  19.           * 每次给两行两列赋值
  20.           *
  21.           * 其中的四个for 循环还可以在次封装  好吧我承认我懒了 每次传递 四个参数就可以了
  22.           *
  23.           * 当我把 偶数 和 奇数的 矩阵打印出来的时候 猛然间发现 原来只要一个 show就可以了  在判断是否是偶数 在确实是否去翻转 不信你自己去试一试
  24.           *
  25.           */
  26.          
  27.          public static void show1(int n)
  28.          {
  29.                     int[][] mat = new int[n][n];
  30.                     int[] max = new int[n];
  31.                     int t = n;
  32.                     for(int i=0;i<n;i++)
  33.                     {
  34.                             max[i] = t*t;
  35.                             t-=2;
  36.                             if(t<=0)break;
  37.                     }
  38.           
  39.                     int Upx=0,Upy=0,Lowx=n-1,Lowy=n-1,Rightx=0,Righty=n-1,Leftx=n-1,Lefty=0;
  40.                     for(int i=0;i<n/2;i++)
  41.                     {
  42.                             if(i!=0)
  43.                             {
  44.                                     Upx += 1; Upy += 1;
  45.                                     Lowx -= 1; Lowy -= 1;
  46.                                     Rightx += 1; Righty -= 1;
  47.                                     Leftx -= 1; Lefty += 1;
  48.                             }
  49.                             mat[Leftx][Lefty] = max[i];
  50.                             for(int y=Lefty+1;y<=Lowy;y++)
  51.                                     mat[Leftx][y] = mat[Leftx][y-1]-1;
  52.                             for(int x=Leftx-1;x>=Rightx;x--)
  53.                                     mat[x][Righty] = mat[x+1][Righty]-1;
  54.                             for(int y=Righty-1;y>=Upy;y--)
  55.                                     mat[Upx][y] = mat[Upx][y+1] -1;
  56.                             for(int x=Upx+1;x<Leftx;x++)
  57.                                     mat[x][Upy] = mat[x-1][Upy] -1;
  58.                     }
  59.                     Print( n, mat);
  60.           }
  61.             

  62.             public static void show2(int n)
  63.             {
  64.                     int[][] mat = new int[n][n];
  65.                     int[] max = new int[n];
  66.                     int t = n;
  67.                     for(int i=0;i<n;i++)
  68.                     {
  69.                             max[i] = t*t;
  70.                             t-=2;
  71.                             if(t<=0)break;
  72.                     }
  73.                     int Rightx=0,Righty=n-1,Upx=0,Upy=0,Leftx=n-1,Lefty=0 ,Lowx=n-1,Lowy=n-1;
  74.                     mat[n/2][n/2]=1;
  75.                     for(int i=0;i<n/2;i++)
  76.                     {
  77.                             if(i!=0)
  78.                             {
  79.                                     Upx += 1; Upy += 1;
  80.                                     Lowx -= 1; Lowy -= 1;
  81.                                     Rightx += 1; Righty -= 1;
  82.                                     Leftx -= 1; Lefty += 1;
  83.                             }
  84.                             mat[Rightx][Righty] = max[i];
  85.                            
  86.                             for(int y=Righty-1;y>=Upy;y--)
  87.                                     mat[Rightx][y] = mat[Rightx][y+1]-1;
  88.                            
  89.                             for(int x=Upx+1;x<=Leftx;x++)
  90.                                     mat[x][Upy] = mat[x-1][Upy]-1;
  91.                             for(int y=Lefty+1;y<=Lowy;y++)
  92.                                     mat[Leftx][y] = mat[Leftx][y-1]-1;
  93.                             for(int x=Lowx-1;x>Rightx;x--)
  94.                                     mat[x][Lowy] = mat[x+1][Lowy]-1;
  95.                     }
  96.                     Print( n, mat);
  97.             }
  98.             /*
  99.              * 就是用来打印数组的  玉圣用 *   来包围数组
  100.              * 怎么实现的自己看代码了
  101.              */
  102.             public static void Print(int n,int[][] mat)
  103.             {
  104.                     for(int i=0;i<n+2;i++)
  105.                         System.out.print("*\t");
  106.                         System.out.println();
  107.                         for(int h=0;h<n;h++)
  108.                         {
  109.                                 System.out.print("*\t");
  110.                                 for(int j=0;j<n;j++)
  111.                                         System.out.print(mat[h][j]+"\t");
  112.                                 System.out.println("*\t");
  113.                         }
  114.                         for(int i=0;i<n+2;i++)
  115.                             System.out.print("*\t");
  116.                             System.out.println();
  117.             }
  118.          
  119. }

  120. public class Main {
  121.         public static void main(String[] args)
  122.         {
  123.                 inVrileMatrix  T = new inVrileMatrix(5);
  124.                 T = new inVrileMatrix(6);
  125.         }

  126. }
复制代码

作者: 陈山洪    时间: 2013-4-30 13:51
看看。。。。
作者: wudongzhe    时间: 2013-4-30 13:53
看看 我来挑战一下:)
作者: 范天成    时间: 2013-4-30 13:56
     看看题
作者: 黑马华    时间: 2013-4-30 13:58
菜鸟也来玩玩
作者: 彭波    时间: 2013-4-30 14:44
又是啥题目啊
作者: 张超    时间: 2013-4-30 14:45
接受自己的挑战
作者: 蓝色骨头    时间: 2013-4-30 17:38
什么题目
作者: 刘健    时间: 2013-4-30 17:43
本帖最后由 刘健 于 2013-4-30 21:50 编辑

看看是不是挑战
看来是
目前成果:支持奇数生成,偶数螺旋方向是反的,没有*号
  1. public class test {
  2.         public static void main(String[] args) {  
  3.             //一共列数
  4.                 int n =4;  
  5.             int[][] pos = new int[n][n];
  6.             
  7.             //圈数,最外围算一圈
  8.             int oic = 0;
  9.             //生成矩形
  10.             for (int i = n; i >=1; i=i-2) {
  11.                         setnum(i, pos, oic);
  12.                         oic++;
  13.                 }
  14.             
  15.             /*输出*/  
  16.             for(int i = 0; i<n; i++){  
  17.                 for(int j = 0; j<n; j++){  
  18.                     if(pos[i][j]<10){  
  19.                         System.out.print(pos[i][j]+" "+" ");  
  20.                     }else{  
  21.                         System.out.print(pos[i][j]+" ");  
  22.                     }  
  23.                 }  
  24.                 System.out.println();  
  25.             }  
  26.         }

  27.         private static void setnum(int n, int[][] pos,int j) {
  28.                 //生成一圈的上面的横排
  29.                 int s = j;
  30.                 for (int i = 0; i < n; i++) {
  31.                         pos[j][s] = n*n -n + 1 + i;
  32.                         s++;
  33.                 }
  34.                
  35.                 //生成一圈的左面排
  36.                 int k = j+1;
  37.                 for (int i = 0; i < n-1; i++) {
  38.                         pos[k][j] = n*n -n -i;
  39.                         k++;
  40.                 }
  41.                
  42.                 //生成一圈的下面排
  43.                 for (int i = 0; i < n-1; i++) {
  44.                         pos[n-1+j][i+1+j] = n*n-n-(n-1)-i;
  45.                 }
  46.                
  47.                 //生成一圈的右面排
  48.                 for (int i = 0; i < n-2; i++) {
  49.                         pos[n-2+j-i][n-1+j] = n*n-n-(n-1)-i-(n-1);
  50.                 }
  51.         }  
  52. }
复制代码

作者: 谭威    时间: 2013-4-30 17:47
la aa a a a aa
作者: 艾萱    时间: 2013-4-30 18:28
回复看看
作者: 邵震    时间: 2013-4-30 18:37
我来看看
作者: 廉哲珉    时间: 2013-4-30 19:19
Yes~!必须挑战!失败过才会知道成功的喜悦
作者: 陈宇鹏    时间: 2013-4-30 19:20
看看。。。。。。。。。。。。
作者: 蓝色骨头    时间: 2013-4-30 19:49
  1. <div class="blockcode"><blockquote>package test;

  2. /**
  3. * 思路:
  4. *          n = 5
  5.            *   *   *   *   *   *   *
  6.            *  21  22  23  24  25   *
  7.            *  20   7   8   9  10   *
  8.            *  19   6   1   2  11   *
  9.            *  18   5   4   3  12   *
  10.            *  17  16  15  14  13   *
  11.            *   *   *   *   *   *   *
  12. *                 经过分析矩阵由两种类型的图形组成
  13. *                 由左上部分
  14. *                         包括左边和上边
  15. *                         1
  16. *
  17.                         7   8   9  
  18.                         6
  19.                         5

  20.                         21  22  23  24  25
  21.                         20
  22.                         19
  23.                         18
  24.                         17  
  25. *                 右下部分
  26. *                         包含右边和下边
  27.                                 2
  28.                         4   3
  29.                                                 10
  30.                                                 11
  31.                                                 12  
  32.                         16  15  14  13  
  33.                 最后外加一层*
  34. *
  35. * @author Administrator
  36. *
  37. */
  38. public class Test{
  39.         private static int x,y;
  40.         private static int[][] arr;
  41.         private static int count;
  42.         /**
  43.          * 顺时针生成左边和上边
  44.          * @param n                边的长度
  45.          */
  46.         private static void left2up(int n){
  47.                 for(int i = 0; i < n; i++){
  48.                         arr[x--][y] = count++;               
  49.                 }
  50.                 n--;
  51.                 x++;
  52.                 y++;
  53.                 for(int i = 0; i < n; i++){
  54.                         arr[x][y++] = count++;       
  55.                 }
  56.         }
  57.         /**
  58.          * 顺时针生成右边和下边
  59.          * @param n                边的长度
  60.          */
  61.         private static void right2down(int n){
  62.                 for(int i = 0; i < n; i++){
  63.                         arr[x++][y] = count++;       
  64.                 }
  65.                 n--;
  66.                 x--;
  67.                 y--;
  68.                 for(int i = 0; i < n; i++){
  69.                         arr[x][y--] = count++;                       
  70.                 }
  71.         }
  72.         /**
  73.          * 从里到外顺时针旋转产生矩阵(长宽相等)
  74.          * @param n                矩阵的规模
  75.          */
  76.         public static void spiral(int n){               
  77.                 //起点
  78.                 x = (n - 1)/2;
  79.                 y = x;
  80.                 arr = new int[n][n];
  81.                 count = 1;
  82.                 for(int i = 1; i <= n; i++){
  83.                         if( i % 2  == 1 ){
  84.                                 left2up(i);
  85.                         }else{
  86.                                 right2down(i);
  87.                         }
  88.                 }
  89.         }
  90.        
  91.        
  92.         //下面的方法用于打印
  93.         /**
  94.          * 计算十进制数字的位数
  95.          * @param num                要计算的数组
  96.          * @return                        返回用十进制表示这个数组时的位数
  97.          */
  98.         private static int count(int num){
  99.                 int cnt = 0;
  100.                 while(num != 0){
  101.                         cnt++;
  102.                         num /= 10;
  103.                 }
  104.                
  105.                 return cnt;
  106.         }
  107.         /**
  108.          * 打印一行*
  109.          * @param m                矩阵的规模
  110.          * @param cnt        最大数字的长度
  111.          */
  112.         private static void printStars(int m, int cnt){
  113.                 StringBuilder sb = new StringBuilder(getSpaces(1, cnt));
  114.                 sb.append("*");               
  115.                 String str = sb.toString();
  116.                 for(int i = 0, len = m+2; i < len; i++){
  117.                         System.out.print(str);
  118.                 }
  119.                 System.out.println();
  120.         }
  121.         /**
  122.          * 生成打印数字之前应该打印的空格
  123.          * @param n                        要打印的数字               
  124.          * @param cnt                数字的最大长度
  125.          * @return                        打印数字之前应该打印的空格
  126.          */
  127.         public static String getSpaces(int n, int cnt){
  128.                 StringBuilder spacesSb = new StringBuilder("  ");       
  129.                 int tmpCnt = count(n);               
  130.                 for(int k = 0; k < cnt - tmpCnt; k++){
  131.                         spacesSb.append(" ");
  132.                 }
  133.                
  134.                 return spacesSb.toString();
  135.         }
  136.         /**
  137.          * 打印生成的二维数组,保证良好的格式,每个数占有的长度相同且随着数组的大小自动调整,
  138.          * 不够的用空格补足。
  139.          *
  140.          * @param matrix        要打印的数组
  141.          * @param m                        数组的行数
  142.          * @param n                        数组的列数
  143.          */
  144.         public static void printMatrix(int[][] matrix, int m, int n){               
  145.                 int cnt = count(m*n);
  146.                 //打印一行"*"               
  147.                 printStars(m, cnt);
  148.                 for(int i = 0; i < m; i++){
  149.                         System.out.print(getSpaces(1, cnt)+"*");
  150.                         for(int j = 0; j < n; j++){                                                       
  151.                                 System.out.print(getSpaces(matrix[i][j], cnt));
  152.                                 System.out.print(matrix[i][j]);
  153.                         }
  154.                         System.out.println(getSpaces(1, cnt)+"*");
  155.                 }
  156.                 //打印一行"*"               
  157.                 printStars(m, cnt);
  158.         }
  159.         public static void main(String[] args){
  160.                 for(int i = 1; i <= 30; i++){
  161.                         System.out.println("n = "+i);
  162.                         spiral(i);
  163.                         printMatrix(arr,i,i);
  164.                         System.out.println();
  165.                 }
  166.         }
  167. }
复制代码

作者: 严露华    时间: 2013-4-30 20:06
试试看了
作者: 孙浩    时间: 2013-4-30 20:38
    看看  
作者: 金辉    时间: 2013-4-30 22:45
挑战,看题~~~
作者: 朱安柱    时间: 2013-4-30 22:45
不尝试,怎么知道自己到底行不行呢?要勇于挑战{:soso_e100:}
作者: 李培根    时间: 2013-4-30 23:18
来支持下,顺便看看题目
作者: 李培根    时间: 2013-4-30 23:19
我的入学测试倒数第二道和这个类似
作者: 黄玉昆    时间: 2013-4-30 23:19
李培根 发表于 2013-4-30 23:18
来支持下,顺便看看题目

培根,还没睡呢啊
作者: 黄玉昆    时间: 2013-4-30 23:20
李培根 发表于 2013-4-30 23:19
我的入学测试倒数第二道和这个类似

就是根据那个改编的,嘿嘿
作者: 马毅    时间: 2013-4-30 23:28
参观参观~~~
作者: 杞文明    时间: 2013-4-30 23:29
看看 呵呵呵              
作者: Lobster    时间: 2013-4-30 23:29
我来看看
作者: yzzshmily    时间: 2013-4-30 23:29
让我看看
作者: 淡蓝色    时间: 2013-4-30 23:29
看看题目
作者: 崔宏奎    时间: 2013-4-30 23:29
第一关会是什么:P
作者: 李易烜    时间: 2013-4-30 23:30
真是的 还要回复!
作者: 清朗的晨风    时间: 2013-4-30 23:31
试试看。。。。。。。。。。。。。
作者: 占琳    时间: 2013-4-30 23:32
写了什么
作者: 黑马-唐磊    时间: 2013-4-30 23:32
看看是神马
作者: 赵崇友    时间: 2013-4-30 23:32
下来看看,尽力做吧!!!1
作者: 谢洋    时间: 2013-4-30 23:32
看看是什么题
作者: 陈雨    时间: 2013-4-30 23:33
又有加分题了,看看
作者: Miss小强    时间: 2013-4-30 23:34
我来试试。。。
作者: shichangyi110    时间: 2013-4-30 23:35
看看是什么东西
作者: 王海龙2013    时间: 2013-4-30 23:37
看看:D:D
作者: ld5128702    时间: 2013-4-30 23:39
kankantimu
作者: HM朱百青    时间: 2013-4-30 23:41
挑战无极限
作者: 愤怒的小鸟    时间: 2013-4-30 23:41
看看!!!
作者: 罗海云    时间: 2013-4-30 23:45
瞧瞧,看看.哈哈
作者: 覃勇    时间: 2013-4-30 23:47
什么题嘛?我来见见世面!
作者: 单单    时间: 2013-4-30 23:52
让我来看看!
作者: 李罡    时间: 2013-4-30 23:53
看看,很给力
作者: 刘赛    时间: 2013-4-30 23:53
标题: RE: =========!!!!!!===敢挑战吗?===!!!!!!=======
只为看题
作者: soulshane    时间: 2013-4-30 23:59
如果是 Java 的,我现在来挑战。算法的也可以。
作者: 覃庆健    时间: 2013-5-1 00:02
回复看题~
作者: 王赟    时间: 2013-5-1 00:39
看看是啥。。
作者: 崔宏奎    时间: 2013-5-1 00:40
本帖最后由 崔宏奎 于 2013-5-1 00:44 编辑

真心没学过矩阵。。。

  1. static void Main(string[] args)
  2.         {
  3.             //计数:
  4.             int start = 1;
  5.             //总数
  6.             int num;
  7.             string str;
  8.             do
  9.             {
  10.                 Console.Write("请输入一个整数:");
  11.                 str = Console.ReadLine();
  12.             } while (!int.TryParse(str, out num) || num < 1);
  13.             //num=num+2;

  14.             string[,] arr = new string[num + 2, num + 2]; //创建数组
  15.             //起始位置:
  16.             int col = (num +1) / 2, row = (num + 1) / 2;  //pos=1;

  17.             //第一圈,特殊
  18.             arr[col, row] = start++.ToString();
  19.             arr[col, ++row] = start++.ToString();
  20.             arr[++col, row] = start++.ToString();
  21.             arr[col, --row] = start++.ToString();
  22.             //第二圈
  23.             for (int i = 1; i < num - 1; ++i)
  24.             {
  25.                 if (i % 2 == 1)   //单数
  26.                 {
  27.                     arr[col, --row] = start++.ToString();
  28.                     //上移
  29.                     for (int j = 0; j <=  i; ++j)
  30.                     {
  31.                         arr[--col, row] = start++.ToString();
  32.                     }
  33.                     //右移
  34.                     for (int j = 0; j <=  i; ++j)
  35.                     {
  36.                         arr[col, ++row] = start++.ToString();
  37.                     }
  38.                 }
  39.                 else   //双数
  40.                 {
  41.                     arr[col, ++row] = start++.ToString();
  42.                     //下移
  43.                     for (int j = 0; j <= i; ++j)
  44.                     {
  45.                         arr[++col, row] = start++.ToString();
  46.                     }
  47.                     //左移
  48.                     for (int j = 0; j <=  i ; ++j)
  49.                     {
  50.                         arr[col, --row] = start++.ToString();
  51.                     }

  52.                 }

  53.             }

  54.             for (int i = 0; i < num + 2;++i )
  55.             {
  56.                 arr[0, i] = "*";
  57.                 arr[i, 0] = "*";
  58.                 arr[i, num+1] = "*";
  59.                 arr[num + 1, i] = "*";
  60.             }
  61.             PrintArr(arr);

  62.             Console.ReadKey();

  63.         }
  64.         public static void PrintArr(string[,] arr)
  65.         {
  66.             int len = arr.GetLength(0);
  67.             for (int i = 0; i < len; ++i)
  68.             {

  69.                 for (int j = 0; j < len; ++j)
  70.                 {
  71.                     Console.Write("{0}\t", arr[i, j]);
  72.                 }
  73.                 Console.WriteLine();
  74.             }
  75.         }
复制代码

作者: 张桂林    时间: 2013-5-1 01:22
看看。。。
作者: 张世钦    时间: 2013-5-1 01:24
本帖最后由 张世钦 于 2013-5-1 01:33 编辑

不错的题目,帮顶了~~~
作者: 叶亮    时间: 2013-5-1 01:30
看看是什么样子的东西。。。
作者: shichangyi110    时间: 2013-5-1 01:35
这个题目的可心就是模拟赋值的路线,根据 n 分别取3,4,5时候,观察数组的赋值路线,摸索出一般的规律
下面是我的代码,鉴于对Java的输入输出还不太熟悉(又不想用字符串数组),下面的输出有点别扭
  1. package cn.xml;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.io.PrintStream;

  9. import org.junit.Test;

  10. public class StreamResult {
  11.        
  12.          
  13.            /**
  14.              *   
  15.              * last :记录当前变法的坐标,这也是整个题目实现的核心
  16.              * step :走的圈数
  17.              * x,y  :代表每圈  的其实位置
  18.              * cnt  :计数统计
  19.              * len  :步长,记录每一圈的步长
  20.              *
  21.              *
  22.             **/
  23.         public   static void main(String args[]) throws Exception
  24.         {
  25.                 int n = 0;
  26.                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  27.                 String num;
  28.                 while( (num = in.readLine())!=null)
  29.                 {
  30.                           try
  31.                           {
  32.                                   n = Integer.parseInt(num);
  33.                           }
  34.                           catch(Exception e)
  35.                           {
  36.                                   System.out.println("您的输入有误哦,请你重新输入");
  37.                                   continue;
  38.                           }
  39.                      
  40.                           if(n<1)
  41.                           {
  42.                                   System.out.println("您的输入有误哦,请你重新输入");
  43.                                   continue;
  44.                           }
  45.                           
  46.                       int map[][] = new int[n+2][n+2];
  47.                      
  48.                           int ct= n/2;
  49.                           if(n%2==1)
  50.                             ct ++;
  51.                           
  52.                           int x,y,last,cnt=1;
  53.                            x = y = last = ct;
  54.                          
  55.                           for(int step = 1; step<=ct; step++)
  56.                           {
  57.                                   int len = step*2 -1;
  58.                                   /** 向上走 **/
  59.                                   
  60.                                   for(int i=0;i<len;i++)
  61.                                   {
  62.                                           last = x-i;
  63.                                           map[last][y] = cnt++;
  64.                                   }
  65.                                   
  66.                                
  67.                                   /** 向右走 **/
  68.                                   x = last;
  69.                                   y = y+1;
  70.                                   for(int i=0;i<len;i++)
  71.                                   {
  72.                                           last = y+i;
  73.                                           map[x][last] = cnt++;
  74.                                   }
  75.                                  
  76.                                   
  77.                                   /** 向下走 **/
  78.                                   
  79.                                   x = x+1;
  80.                                   y = last;
  81.                                   for(int i=0;i<len;i++)
  82.                                   {
  83.                                           last = x +i;
  84.                                           map[last][y] = cnt++;
  85.                                   }
  86.                                   
  87.                                   /** 向左走 **/
  88.                                   
  89.                                   x = last;
  90.                                   y = y-1;
  91.                                   for(int i=0;i<len;i++)
  92.                                   {
  93.                                           last = y-i;
  94.                                           map[x][last] = cnt++;
  95.                                   }
  96.                                   y = last-1;
  97.                                   
  98.                           }
  99.                           
  100.                   
  101.                   /** 输出结果,表示很别扭 **/
  102.                  
  103.                   for(int i=0;i<=n+1;i++)
  104.                   {
  105.                         if(i==0 || i==n+1)
  106.                                 {
  107.                                   for(int j=0;j<=n+1;j++)
  108.                                           System.out.printf("%-5c",'*');
  109.                                 }
  110.                                
  111.                         else
  112.                         {
  113.                                 for(int j=0;j<=n+1;j++)
  114.                                 {
  115.                                         if(j==0 || j==n+1)
  116.                                                 System.out.printf("%-5c",'*');
  117.                                         else
  118.                                         {
  119.                                                 System.out.printf("%-5d", map[i][j]);
  120.                                         }
  121.                                        
  122.                                 }
  123.                         }
  124.                        
  125.                         System.out.println("\n");
  126.                        
  127.                   }
  128.                 }
  129.                
  130.         }

  131.    
  132. }
复制代码
运行结果:
1
*    *    *   

*    1    *   

*    *    *   

3
*    *    *    *    *   

*    7    8    9    *   

*    6    1    2    *   

*    5    4    3    *   

*    *    *    *    *   

4
*    *    *    *    *    *   

*    7    8    9    10   *   

*    6    1    2    11   *   

*    5    4    3    12   *   

*    16   15   14   13   *   

*    *    *    *    *    *   

5
*    *    *    *    *    *    *   

*    21   22   23   24   25   *   

*    20   7    8    9    10   *   

*    19   6    1    2    11   *   

*    18   5    4    3    12   *   

*    17   16   15   14   13   *   

*    *    *    *    *    *    *   

0
您的输入有误哦,请你重新输入


作者: 杞文明    时间: 2013-5-1 02:40
本帖最后由 杞文明 于 2013-5-1 02:44 编辑

弄了一哈! 哎!  好麻烦哦!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 数组的题
{
class Program
{
static void Main(string[] args)
{

/*
思想:如果我们求的二维数组的行数是偶数,那么一个外围中大小一个是这样的
* 下边→右边→上边→左边 例如:行数是4的数的二维数组
* 7 8 9 10
* 6 1 2 11
* 5 4 3 12
* 16 15 14 13
* 他们从大到小是 16 15 14 → 13 12 11 → 10 9 8 → 7 6 5
* 用这种方式一层一层的给二位数组赋值
*
*
如果我们求的二维数组的行数是奇数数,那么一个外围中大小一个是这样的
* 上边→左边→下边→右边 例如:行数是3的数的二维数组
* 7 8 9
* 6 1 2
* 5 4 3
* 他们从大到小是 9 8 → 7 6 → 5 4 → 3 2
* 用这种方式一层一层的给二位数组赋值
*/

Console.WriteLine("输入一个数");
//转换为int型
int num =Convert.ToInt32( Console.ReadLine());
//定义数组
int[,]shuzu = new int[num,num];

int xxh = num-1; //用去记住我们要循环的开始位置和结束位置
int num1 = num; //用于求最大值
//循环的次数是行数的一半
for (int w = (num + 1) / 2; w >= 1; w--)
{
#region 偶数
if (num % 2 == 0)
{
//本次循环的最大值
int zuida = num1 * num1;

//下边
for (int j = num - 1 - xxh; j < xxh; j++)
{//下边:从左往右赋值 依次减小1
shuzu[xxh, j] = zuida;
zuida--;
}

//右边
for (int j = xxh; j > num - 1 - xxh; j--)
{//右边:从下往上赋值 依次减小1
shuzu[j, xxh] = zuida;
zuida--;
}

//上面
for (int j = xxh; j > num - 1 - xxh; j--)
{ //上边:从右往左赋值 依次减小1
shuzu[num - 1 - xxh, j] = zuida;
zuida--;
}

//左边
for (int j = num - 1 - xxh; j < xxh; j++)
{//左边:从上往下赋值 依次减小1
shuzu[j, num - 1 - xxh] = zuida;
zuida--;
}
xxh--; //开始和结束位置向中间靠拢
num1 = num1 - 2; //前一个和后一个偶数相差2
}
#endregion

#region 奇数
/*和偶数的循环同理 只是 他的大小事这样的: 上边→左边→下边→右边*/
if (num % 2 == 1)
{
int zuida = num1 * num1;
//上面一行
for (int j = xxh; j >= num - 1 - xxh; j--)
{
shuzu[num - 1 - xxh, j] = zuida;
zuida--;
}
//最左边
zuida++;
for (int j = num - 1 - xxh; j < xxh; j++)
{
shuzu[j, num - 1 - xxh] = zuida;
zuida--;
}

//最后一行

for (int j = num - 1 - xxh; j <= xxh; j++)
{
shuzu[xxh, j] = zuida;
zuida--;
}

//最右边这排
zuida++;
for (int j = xxh; j > num - 1 - xxh; j--)
{
shuzu[j, xxh] = zuida;
zuida--;
}

xxh--;
num1 = num1 - 2;

}
#endregion

}

//这个for是显示第一行的*号的
for (xxh = 0; xxh < num+2; xxh++)
{
Console.Write("*\t");
}

//这个for是显示数据和显示数据前面和后面的*
for (int k = 0; k < num; k++)
{
Console.WriteLine();
Console.WriteLine();
Console.Write("*\t");
for (int l = 0; l < num; l++)
{
Console.Write("{0}\t",shuzu[k, l]);
}
Console.Write("*");
}

//数据完成后,换一哈行
Console.WriteLine("\n\t");

//这个for是显示最后一行的*号的
for ( xxh = 0; xxh < num + 2; xxh++)
{
Console.Write("*\t");
}

Console.ReadKey();
}
}
}




作者: 何俊森    时间: 2013-5-1 07:41
rhrh看看了。。
作者: by354641572    时间: 2013-5-1 07:48
看看,学习学习
作者: 夏保森    时间: 2013-5-1 07:56
谢谢楼主的贡献,支持
作者: 王梦南    时间: 2013-5-1 08:35
过来看一看题

作者: 黑马田杰    时间: 2013-5-1 08:44
挑战一下
作者: 清朗的晨风    时间: 2013-5-1 08:45
本帖最后由 清朗的晨风 于 2013-5-1 09:00 编辑

终于写好了,完全自己写的,没有任何百度。。。希望滔哥指点下。
  1. class Program
  2.     {
  3.         static int find(int i, int j, int n)
  4.         {
  5.             if (n % 2 == 1)//边长为奇数时调用这个
  6.             {
  7.                 if (i == 1) return n * (n - 1) + j;
  8.                 if (j == 1) return n * (n - 1) + 2 - i;
  9.                 if (i == n) return (n-1) * (n-1) - j + 2;
  10.                 if (j == n) return (n-2) * (n - 2)-1 + i;
  11.                 return find(i - 1, j - 1, n - 2);//递归调用内圈
  12.             }
  13.             else//边长为偶数数时调用这个
  14.             {            
  15.                 if (i == n) return n * n - j + 1;
  16.                 if (j == n) return n * n - 2 * n + 1 + i;
  17.                 if (i == 1) return (n -1)* (n - 2) + j;
  18.                 if (j == 1) return (n - 1) * (n - 2) + 2 - i;
  19.                 return find(i-1, j-1, n-2);//递归调用内圈
  20.             }
  21.         }

  22.         static void Main(string[] args)
  23.         {
  24.             int n, i, j;
  25.             Console.Write("请输入一个数字:");
  26.             if (Int32.TryParse(Console.ReadLine(), out n))
  27.             {
  28.                 int[,] number = new int[n + 2, n + 2];//定义个二维数组
  29.                 for (i = 1; i <= n; i++)
  30.                     for (j = 1; j <= n; j++)
  31.                         number[i, j] = find(i, j, n);

  32.                 for (i = 0; i <= n + 1; i++)
  33.                 {
  34.                     for (j = 0; j <= n + 1; j++)
  35.                     {
  36.                         if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
  37.                             Console.Write("  * ");//打印最外层的*
  38.                         else
  39.                             Console.Write("{0,4}", number[i, j]);
  40.                     }
  41.                     Console.WriteLine();
  42.                 }
  43.                 Console.ReadKey();
  44.             }
  45.             else
  46.                 Console.WriteLine("输入的不是数字");
  47.         }
  48.     }
复制代码

作者: 贺骏鹏    时间: 2013-5-1 09:03
看题。。。。。。。。。。。。。。。。。。。。
作者: 泓谅居士    时间: 2013-5-1 09:41
尝试一下。
作者: 黄玉昆    时间: 2013-5-1 10:18
陈圳 发表于 2013-4-30 10:40

这个设置作者可见,我到时候有空告诉你啊,你也可以私下问我,现在有点忙,嘿嘿
作者: 曹睿翔    时间: 2013-5-1 10:31
曹睿翔 发表于 2013-4-30 13:13
我来晚了啊,玉圣!时间不对吧,48小时not equal to今天十点到明天吧9点吧,我还在店里,回宿舍看看
我的网 ...

代码稀少?还是觉得太多代码了,没来得及继续抽取
需要的话我画出图形步骤,以前没做个这类型题,不知道解决的最简单的思路,有好的发来看看
测试代码不去为好,自己回头看可以知道怎么解决问题的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2