本帖最后由 fcyan86 于 2013-6-4 15:52 编辑
昨天在黑马看到个这人题,要求如下图所示旋转输出
我的实现代码如下:- private static void ShowNumber(int n)
- {
- Console.WriteLine("得到的图形为:");
- int[,]showArr=new int[n,n];
- for (int i = n,j=0; i > 0; i -= 2,j++)
- {
- for (int k = (n-i)/2,m=i*(i-1)+1; k < i+(n-i)/2; k++,m++)
- {
- showArr[j,k]=m;
- }
- }
- for (int i = 0; i < n/2 + 1; i++)
- {
- for (int j = i, m = showArr[i, i];j<n-i;m--,j++)
- {
- showArr[j, i] = m;
- }
- }
- for (int i = n/2; i<n; i++)
- {
- for (int j = n-i-1, m = showArr[i, n-i-1]; j < i+1; m--,j++)
- {
- showArr[i, j] = m;
- }
- }
- for (int i = n - 1; i > n/2; i--)
- {
- for(int j=i,m=showArr[i,i];j>n-i-1;j--,m--)
- {
- showArr[j, i] = m;
- }
- }
- StringBuilder showStr=new StringBuilder();
- for (int i = 0; i < n + 2; i++)
- {
- showStr.Append("*\t");
- }
- Console.WriteLine(showStr);
- for (int i = 0; i < n; i++)
- {
- Console.Write("*\t");
- for(int j=0;j<n;j++)
- Console.Write(showArr[i,j]+"\t");
- Console.WriteLine("*");
- }
- Console.WriteLine(showStr);
- }
复制代码- Console.WriteLine("请输入n的值:");
- int n = 0;
- do
- {
- if (int.TryParse(Console.ReadLine(), out n)&&n%2!=0)
- break;
- else
- {
- Console.WriteLine("输入的n必须为奇数,请重新输入:");
- }
- } while (true);
- ShowNumber(n);
复制代码 各位大神们有没有简单些的
|
|