螺旋状输出

 

总觉得做得很麻烦……

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. namespace test3_1   
  7. {   
  8.     class Program   
  9.     {   
  10.         static void right(int[,] a, int t)//右移校正:数组a,a的长度t   
  11.         {   
  12.             for (int i = t; i > 0; i--)   
  13.                 for (int j = t; j > 0; j--)   
  14.                 {   
  15.                     a[i, j] = a[i - 1, j - 1];   
  16.                     print(t, a);   
  17.                 }   
  18.   
  19.         }   
  20.         static void print(int i, int[,] a) //打印数组:数组a,a的长度i   
  21.         {   
  22.             for (int m = 0; m < i; m++)   
  23.             {   
  24.                 for (int n = 0; n < i; n++)   
  25.                 {   
  26.                     if (a[n, m] < 10)   
  27.                         Console.Write(" ");   
  28.                     Console.Write(a[n, m]+" ");                       
  29.                 }   
  30.                 Console.WriteLine();   
  31.             }   
  32.         }   
  33.         static void xy(int q,int[,] a,int i)//整理数据:数字q,数组a,a的长度i   
  34.         {   
  35.             int x=0 , y =0;//数字位置   
  36.             for (int z = 1; z < i; z++)   
  37.             {   
  38.                 if (z % 2 == 1)   
  39.                 {   
  40.                     for (++x ; x <= z; x++)//向右   
  41.                     {   
  42.                         a[x, 0] = q++;   
  43.                         Console.WriteLine("向右:{0},{1},{2}", a[x, 0], x, 0);   
  44.                     }   
  45.                     for (--x ; y < z; y++)//向下   
  46.                     {   
  47.                         a[x, y+1 ] = q++;   
  48.                         Console.WriteLine("向下:{0},{1},{2}", a[x, y+1], x, y+1);   
  49.                     }   
  50.                     for (--x; x >= 0; x--)//向左   
  51.                     {   
  52.                         a[x, y] = q++;   
  53.                         Console.WriteLine("向左:{0},{1},{2}", a[x, y], x, y);   
  54.                     }   
  55.                     print(i, a);   
  56.                 }   
  57.                 else  
  58.                 {   
  59.                     right(a, a.GetLength(1) - 1);   
  60.                     for (++x,++y  ; x >= 0; x--)//向左   
  61.                     {   
  62.                         a[x, y] = q++;   
  63.                         Console.WriteLine("向左:{0},{1},{2}", a[x, y], x, y);   
  64.                     }   
  65.                     for (++x; y > 0; y--)//向上   
  66.                     {   
  67.                         a[x, y-1] = q++;   
  68.                         Console.WriteLine("向上:{0},{1},{2}", a[x, y-1], x, y-1);   
  69.                     }   
  70.                     for (; x < z; x++)//向右   
  71.                     {   
  72.                         a[x+1, y] = q++;   
  73.                         Console.WriteLine("向右:{0},{1},{2}", a[x+1, y], x+1, y);   
  74.                     }   
  75.                     print(i, a);   
  76.                 }   
  77.             }   
  78.         }   
  79.         static void Main(string[] args)   
  80.         {   
  81.             int i ;   
  82.             Console .Write ("请输入i:");   
  83.             i=Convert .ToInt16 (Console .ReadLine ());   
  84.             int[,] myarray = new int[i, i];//二元数组表示方阵   
  85.             myarray[0, 0] = 1;   
  86.             int q = 2;   
  87.             xy(q, myarray , i);   
  88.             Console.ReadKey();   
  89.         }   
  90.     }   
  91. }   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值