码迷,mamicode.com
首页 > 其他好文 > 详细

螺旋矩阵

时间:2020-03-27 17:05:14      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:lse   void   style   als   print   color   bsp   没有   fun   

终止条件还有没有其他的那?

我将,是否被填满作为终止条件。

还有没有其他的那?欢迎评论。

#include<stdio.h>
#define N 100
int arr[N][N];
const int direction_i[4] = {0,1,0,-1};
const int direction_j[4] = {1,0,-1,0};
bool check(int n,int up_edge, int right_edge,int down_edge, int left_edge)
{
    if(up_edge > n/2 || left_edge == n/2)
    return true;
    else return false;
}
void fun(int a[][N],int n)
{
    int up_edge = 1, right_edge = n-1, down_edge = n-1, left_edge = 0;
    int i = 0, j = 0;
    int cnt = 1;
    a[i][j] = 1;
    cnt++;
    int direction_index = 0;
    while(cnt <= n*n)
    {
        if(j == right_edge && direction_index == 0)
        {
            direction_index = 1;
            right_edge--;
        }
        else if( i == down_edge && direction_index == 1)
        {
            direction_index = 2;
            down_edge--;
        }
        else if( j == left_edge && direction_index == 2)
        {
            direction_index = 3;
            left_edge++;
        }
        else if( i == up_edge && direction_index == 3)
        {
            direction_index = 0;
            up_edge++;
        }
        i += direction_i[direction_index];
        j += direction_j[direction_index];
        a[i][j] = cnt++;
    }
}
void output(int a[][N],int n)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
            printf("%-4d%c",a[i][j],(j==n-1)?\n: );
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        fun(arr,n);
        output(arr,n);
    }


    return 0;
}

 

螺旋矩阵

标签:lse   void   style   als   print   color   bsp   没有   fun   

原文地址:https://www.cnblogs.com/zhang-zsq/p/12582322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!