n行m列矩阵顺时针填写1~n*m

程序效果图如下:

程序参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>//自己写的code
#include <stdlib.h>
#define N 20
//解决的问题:数字从1开始顺时针填入n行m列数组
int  arrary[N][N]={0};
void  assist( int , int );
void  deal( int  &, int , int );
int  main()
{
int  row=1,column=1,i=row,j=column,n,m;
     int  count=0,num=1;
     int  a,b;
     printf ( "请输入旋转阵的行和列:" );
     scanf ( "%d%d" ,&n,&m);
     a=n;b=m;
assist(n,m);
if (n%2) a++;
if (m%2) b++;
while (count!=(n>m? b/2 : a/2))
{
count++;
deal(num,row,column);
row++;column++;
}
     for (i=1;i<=n;i++){
         for (j=1;j<=m;j++)
             printf ( "%4d " ,arrary[i][j]);
         printf ( "\n" );
     }
     return  0; 
}
void  assist( int  n, int  m) //外加围墙
    for ( int  j=0;j<=m+1;j++)
    arrary[0][j]=1;
    for ( int  i=0;i<=n+1;i++)
    arrary[i][m+1]=1;
    for ( int  j=m+1;j>=0;j--)
     arrary[n+1][j]=1;
    for ( int  i=n+1;i>=0;i--)
     arrary[i][0]=1;
}
void  deal( int  &num, int  column, int  row)
{
int  i,j;
i=row;j=column;
for (j=column;;j++) //右横
{
     if (arrary[row][j]) break ;
arrary[row][j]=num++;
}column=j-1;
// printf("column:%d\n",column);测试数据时候检测
for (i=row+1;;i++) //下
{
if (arrary[i][column]) break ;
arrary[i][column]=num++;
}row=i-1;
// printf("row:%d\n",row);
for (j=column-1;;j--) //左横
{
     if (arrary[row][j]) break ;
arrary[row][j]=num++;
}column=j+1;
// printf("column:%d\n",column);
for (i=row-1;;i--) //上
{
if (arrary[i][column]) break ;
arrary[i][column]=num++;
}row=i-1;
// printf("row:%d\n",row);
}

 

转载于:https://www.cnblogs.com/cstdio1/p/10903090.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值