练习6

 
  1. /************************************************************************
  2. 6. 矩阵中填数. 当给出 N*N 的矩阵,要求用程序填入下列形式的数:
  3.    ① 倒填,例如N=5             ② 蛇形填数              ③ 回转填数
  4.  ┌─┬─┬─┬─┬─┐   ┌─┬─┬─┬─┬─┐   ┌─┬─┬─┬─┬─┐
  5.  │25│24│23│22│21│   │ 1│ 3│ 4│10│11│   │ 1│16│15│14│13│
  6.  ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
  7.  │20│19│18│17│16│   │ 2│ 5│ 9│12│19│   │ 2│17│24│23│12│
  8.  ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
  9.  │15│14│13│12│11│   │ 6│ 8│13│18│20│   │ 3│18│25│22│11│
  10.  ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
  11.  │10│ 9│ 8│ 7│ 6│   │ 7│14│17│21│24│   │ 4│19│20│21│10│
  12.  ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
  13.  │ 5│ 4│ 3│ 2│ 1│   │15│16│22│23│25│   │ 5│ 6│ 7│ 8│ 9│
  14.  └─┴─┴─┴─┴─┘   └─┴─┴─┴─┴─┘   └─┴─┴─┴─┴─┘
  15.   ***********************************************************************/
  16. #include <stdio.h>
  17. //打印
  18. void output(int** array, int n)
  19. {
  20.     int row,col;
  21.     for(row=0; row<n; row++)
  22.     {
  23.         for(col=0; col<n; col++)
  24.         {
  25.             printf("%4d",*((int*)array + n*row + col));
  26.         }
  27.         printf("/n");
  28.     }
  29. }
  30. //倒填
  31. void O_Fill(int** array, int n)
  32. {
  33.     int row,col;
  34.     int counter = 1;
  35.     for(row=n-1; row>=0; row--)
  36.         for(col=n-1; col>=0; col--)
  37.         {
  38.             *((int*)array + n*row + col) = counter++; 
  39.         }
  40. }
  41. //蛇形
  42. void S_Fill(int** array, int n)
  43. {
  44.     bool direct=false;
  45.     int row,col;
  46.     int counter = 1;
  47.     int n2 = n*n;
  48.     row = 0; col = 0;
  49.     *((int*)array + n*row + col) = counter;
  50.     for(++counter; counter<=n2; counter++)
  51.     {
  52.         if(!direct)
  53.         {
  54.             row++;
  55.             col--;
  56.             if(row==n)
  57.             {
  58.                 row--;
  59.                 col+=2;
  60.                 direct = !direct;
  61.             }
  62.             else if(col<0)
  63.             {
  64.                 col++;
  65.                 direct = !direct;
  66.             }
  67.         }
  68.         else
  69.         {
  70.             row--;
  71.             col++;
  72.             if(col==n)
  73.             {
  74.                 row+=2;
  75.                 col--;
  76.                 direct = !direct;
  77.             }
  78.             else if(row<0)
  79.             {
  80.                 row++;
  81.                 direct = !direct;
  82.             }
  83.         }
  84.         *((int*)array + n*row + col) = counter;
  85.     }
  86. }
  87. //回形
  88. void H_Fill(int** array, int n)
  89. {
  90.     int row,col;
  91.     int counter;
  92.     int n2 = n*n;
  93.     bool direct = false;
  94.     row = 0, col = 0;
  95.     counter = 1;
  96.     *((int*)array + n*row + col) = counter;
  97.     for(++counter; counter<=n2; counter++)
  98.     {
  99.         if(!direct)
  100.         {
  101.             row++;
  102.             if(row==n)
  103.             {
  104.                 row--;
  105.                 col++;
  106.                 direct = !direct;
  107.             }
  108.         }
  109.         else
  110.         {
  111.             row--;
  112.             if(row<0)
  113.             {
  114.                 row++;
  115.                 col++;
  116.                 direct = !direct;
  117.             }
  118.         }
  119.         *((int*)array + n*row + col) = counter; 
  120.     }
  121.     
  122. }
  123. void main()
  124. {
  125.     int array[6][6];
  126.     O_Fill((int**)array,6);
  127.     output((int**)array,6);
  128.     printf("/n");
  129.     S_Fill((int**)array,6);
  130.     output((int**)array,6);
  131.     printf("/n");
  132.     H_Fill((int**)array,6);
  133.     output((int**)array,6);
  134. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值