A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王震 中级黑马   /  2013-6-22 23:31  /  2185 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

输入一个数,然后显示这样规律的图怎样做
1 2
4 3


7 8 9
6 1 2
5 4 3




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

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

4 个回复

倒序浏览
这是一个数字按照逆时针填充的的图像。
一个数字包含三个信息,它的值、x坐标和y坐标
令数字1的坐标为(0,0).
那么他的下一个数2,填充到其右边的位置,坐标为(1,0)
这时就要检查2的逆时针位置(1,-1)是否有数字存在,
如果有,则3应填充在2的右边(2,0),如果没有,则3填充在2的下边(1,-1)。
以此类推,检查当前数字的逆时针位置,
如果已经有数字存在,则按照当前数字的填充方向,继续填充
如果没有数字存在,则填充到逆时针位置
将所有数字的值和坐标信息保存在列表中
输出时,先输出最上面的一列(y坐标最大),从x坐标最小的数开始输出
换行后,输出y坐标减1的那一列,也是从x坐标最小的数开始输出
直到输出全部的数字
就可以达到楼主的效果
回复 使用道具 举报
本帖最后由 黄文超 于 2013-6-23 20:18 编辑
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             
  12.             //坐标极限值
  13.             int xMin = 0;
  14.             int xMax = 0;
  15.             int yMin = 0;
  16.             int yMax = 0;
  17.             //表,用于保存数字坐标
  18.             List<NumInfo> numList =new List<NumInfo>();
  19.             //当前数字
  20.             NumInfo currentNum=new NumInfo();
  21.             //当前数字逆时针位置的数字
  22.             NumInfo nextNum = new NumInfo();
  23.             //数字的填充方向
  24.             MoveTo move = MoveTo.Right;
  25.             
  26.             Console.WriteLine("请输入一个数字");
  27.             //获取输入的数字
  28.             int numGet = 0;
  29.             try
  30.             {
  31.                 numGet = int.Parse(Console.ReadLine());
  32.             }
  33.             catch(Exception e)
  34.             {
  35.                 Console.WriteLine(e.Message);
  36.             }
  37.             //给每个数字添加坐标
  38.             for (int i = 1; i <= numGet; i++)
  39.             {
  40.                 if (i == 1)
  41.                 {
  42.                     currentNum.Num = i;
  43.                     currentNum.x = 0;
  44.                     currentNum.y = 0;
  45.                     numList.Add(currentNum);
  46.                     //指定下一次数字出现的方向
  47.                     move = MoveTo.Right;
  48.                 }
  49.                 else
  50.                 {
  51.                     currentNum.Num = i;
  52.                     nextNum = currentNum;
  53.                     switch (move)
  54.                     {
  55.                         case MoveTo.Right:
  56.                             currentNum.x++;//向右填充,x增加
  57.                             if (xMax < currentNum.x)
  58.                             {
  59.                                 xMax = currentNum.x;//保存x的最大值
  60.                             }
  61.                             nextNum.x=currentNum.x;//因为当前向右填充,所以检查下方位置,x不变
  62.                             nextNum.y--;//因为当前向右填充,所以检查下方位置,y减1
  63.                             break;
  64.                         case MoveTo.Down:
  65.                             currentNum.y--;
  66.                             if (yMin > currentNum.y)
  67.                             {
  68.                                 yMin = currentNum.y;
  69.                             }
  70.                             nextNum.y = currentNum.y;
  71.                             nextNum.x--;
  72.                             break;
  73.                         case MoveTo.Left:
  74.                             currentNum.x--;
  75.                             if (xMin > currentNum.x)
  76.                             {
  77.                                 xMin = currentNum.x;
  78.                             }
  79.                             nextNum.x = currentNum.x;
  80.                             nextNum.y++;
  81.                             break;
  82.                         case MoveTo.Up:
  83.                             currentNum.y++;
  84.                             if (yMax < currentNum.y)
  85.                             {
  86.                                 yMax = currentNum.y;
  87.                             }
  88.                             nextNum.y = currentNum.y;
  89.                             nextNum.x++;
  90.                             break;
  91.                     }
  92.                     //将当前数字的信息添加到列表中
  93.                     numList.Add(currentNum);

  94.                     //检查逆时针方向的下一个点有无点,若无,则变化填充方向。
  95.                     for (int j = 0; j < numList.Count; j++)
  96.                     {
  97.                         //有一个点在逆时针方向的下一个点的位置
  98.                         if ((nextNum.x == numList[j].x) && (nextNum.y == numList[j].y))
  99.                         {
  100.                             break;
  101.                         }
  102.                             //不存在这样的点,改变填充方向
  103.                         else if (j == numList.Count-1)
  104.                         {
  105.                             switch (move)
  106.                             {
  107.                                 case MoveTo.Right:
  108.                                     move = MoveTo.Down;//右逆时针旋转后为向下
  109.                                     break;
  110.                                 case MoveTo.Down:
  111.                                     move = MoveTo.Left;
  112.                                     break;
  113.                                 case MoveTo.Left:
  114.                                     move = MoveTo.Up;
  115.                                     break;
  116.                                 case MoveTo.Up:
  117.                                     move = MoveTo.Right;
  118.                                     break;
  119.                             }
  120.                         }
  121.                     }
  122.                 }
  123.             }
  124.             //数字坐标生成完毕
  125.             //输出数字
  126.             for (int y = yMax; y >= yMin; y--)
  127.             {
  128.                 for (int x = xMin; x <= xMax; x++)
  129.                 {
  130.                     //查找坐标为(x,y)的数字并输出,若无,则用空格填充
  131.                     string tempNum=null;

  132.                     for (int n = 0; n < numList.Count; n++)
  133.                     {
  134.                         if (numList[n].x == x && numList[n].y == y)
  135.                         {
  136.                             tempNum = numList[n].Num.ToString();
  137.                             numList.RemoveAt(n);
  138.                             break;
  139.                         }
  140.                         else if (numList.Count - 1 == n)
  141.                         {
  142.                             tempNum = " ";
  143.                         }
  144.                     }
  145.                     Console.Write(tempNum);
  146.                     if (x < xMax)
  147.                     {
  148.                         Console.Write("\t");
  149.                     }
  150.                 }
  151.                 Console.WriteLine();
  152.             }
  153.             Console.ReadLine();
  154.         }
  155.     }
  156.     //定义一个结构体用于记录数字的信息
  157.     struct NumInfo
  158.     {
  159.         public int Num;
  160.         public int x;
  161.         public int y;
  162.     }
  163.     enum MoveTo { Right, Down, Left, Up }
  164. }
  165. //这是参考代码
复制代码
回复 使用道具 举报
楼上的你
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace 螺旋矩阵
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             /*
  12.              思路:
  13.              从样例给中的矩阵也就是n=7的时候,我们可以发现以下规律:
  14.              * //第一圈
  15.              * 1向右走1步就可以达到2
  16.              * 然后2向下走1步可以达到3
  17.              * 然后3向左走2步可以到5
  18.              * 然后5向上走2步可以达到7
  19.              //第二圈
  20.               * 7向右走3步就可以达到10
  21.              * 然后10向下走3步可以达到13
  22.              * 然后13向左走4步可以到17
  23.              * 然后17向上走4步可以达到21
  24.              ==============================
  25.               得出
  26.               向右走的步数为1,3,5,7 ...2*n+1;
  27.               向下走的步数为1,3,5,7 ...2*n+1;
  28.               向左走的步数为2,4,6,8 ...2*n;
  29.               向上走的步数为2,4,6,8 ...2*n;
  30.               然后就是根据此规律得出一个螺旋矩阵
  31.             
  32.              *
  33.              */


  34.             Console.WriteLine("请输入n的值");
  35.             string str = Console.ReadLine();
  36.             int n, i, j;
  37.             //定义一个二维数组a
  38.             int [,]a=new int[100,100];
  39.             //定义一个string数组s
  40.             string[,] s = new string[100, 100];
  41.             bool flag=int.TryParse(str, out n);
  42.             if (flag)
  43.             {
  44.                 //数组初始化
  45.                 for ( i = 1; i <=n; i++)
  46.                 {
  47.                     for (j = 1; j <=n; j++)
  48.                     {
  49.                         a[i, j] = 0;                    
  50.                     }
  51.                 }
  52.                 //posI,posJ表示1的初始位置
  53.                 int posI = (n + 1) / 2;
  54.                 int posJ = (n + 1) / 2;
  55.                 a[posI, posJ] = 1;
  56.                 int k = 1,f=0;//k表示从1开始到n*n,f计数
  57.                 while (k<=n*n)
  58.                 {
  59.                     //向右1 3  5  7
  60.                     for (i = 1; i <= 2 * f + 1; i++)
  61.                         a[posI, ++posJ] = ++k;                             
  62.                     //向下1 3  5  7
  63.                     for (i = 1; i <= 2 * f + 1; i++)
  64.                         a[++posI, posJ] = ++k;

  65.                     //向左2 4  6  8
  66.                     for (i = 1; i <= 2 *(f+1) ; i++)
  67.                         a[posI, --posJ] = ++k;

  68.                     //向上2 4  6  8
  69.                     for (i = 1; i <= 2 *(f + 1); i++)
  70.                         a[--posI, posJ] = ++k;
  71.                     f++;

  72.                 }            
  73.                 Console.WriteLine("得到的图形为:");
  74.                 for (i = 0; i <n + 2; i++)
  75.                     s[0, i] = "*";        
  76.                 for (i = 1; i <= n; i++)
  77.                 {
  78.                     s[i, 0] = "*";
  79.                     for (j = 1; j <= n; j++)
  80.                     {
  81.                         
  82.                         s[i, j] = a[i, j].ToString();                        
  83.                     }
  84.                     s[i , n+1] = "*";            
  85.                 }
  86.                 for (i = 0; i < n + 2; i++)
  87.                     s[n+1, i] = "*";
  88.                 for (i = 0; i < n + 2; i++)
  89.                 {
  90.                     for (j = 0; j <n + 2; j++)
  91.                     {
  92.                         Console.Write("{0,-8}",s[i,j]);              
  93.                     }
  94.                     Console.WriteLine();
  95.                 }
  96.             }
  97.            
  98.         }
  99.     }
  100. }
复制代码
回复 使用道具 举报
好难的说,完全不懂,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马