• 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(" ");
        }
        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 ",column);测试数据时候检测
    for(i=row+1;;i++)//下
    {
    if(arrary[i][column])break;
    arrary[i][column]=num++;
    }row=i-1;
    // printf("row:%d ",row);
    for(j=column-1;;j--)//左横
    {
        if(arrary[row][j])break;
    arrary[row][j]=num++;
    }column=j+1;
    // printf("column:%d ",column);
    for(i=row-1;;i--)//上
    {
    if(arrary[i][column])break;
    arrary[i][column]=num++;
    }row=i-1;
    // printf("row:%d ",row);
    }

     

    不一样的烟火
  • 相关阅读:
    ThreadSafety with the AutoResetEvent, ManualResetEvent Class(Synchronization of .net)
    使用Python SMTP发送邮件
    flask项目中设置logo
    如何解决Bootstrap中分页不能居中的问题
    pip install mysql_python报错解决办法
    git上拉项目
    AttributeError: 'str' object has no attribute 'decode'
    pycharm设置SDK
    为git创建远程仓库
    开发过程中git的使用
  • 原文地址:https://www.cnblogs.com/cstdio1/p/10903090.html
Copyright © 2020-2023  润新知