zoukankan      html  css  js  c++  java
  • 蛇形矩阵

     1 #include <iostream>
     2 using namespace std;
     3 int a[100][100]={0};
     4 int main()
     5 {
     6      
     7     int n,x,y,t=1;
     8     cin>>n;
     9     memset(a,0,sizeof(a));
    10     x=0;
    11     y=n-1;
    12     a[x][y]=1;
    13     while(t<n*n)
    14     {
    15         while(x+1<n&&!a[x+1][y])       
    16             a[++x][y]=++t;
    17         while(y-1>=0&&!a[x][y-1])
    18             a[x][--y]=++t;
    19         while(x-1>=0&&!a[x-1][y])
    20             a[--x][y]=++t;
    21         while(y+1<n&&!a[x][y+1])
    22             a[x][++y]=++t;
    23     }
    24     for(int i=0;i<n;i++)
    25     {
    26         cout<<endl;
    27         for(int j=0;j<n;j++)
    28             cout<<a[i][j]<<" ";
    29     }
    30     cout<<endl;
    31     return 0;
    32 }
    33 
    34 /*while循环主要用来判断是否可以继续前进(这里的前进是指相对于某一个点,其实际的前进方向);
    35 可以继续前进必须满足两个条件,没有超出矩阵的范围,前进的方向没有被填入数字
    36 */
    View Code

    参考来源:https://blog.csdn.net/Liuchang54/article/details/44838513

  • 相关阅读:
    leetcode 34 rust
    leetcode 2 rust
    leetcode 1 rust
    leetcode 20 rust
    leetcode 287 rust
    leetcode 18 rust
    lottery抽奖
    webpack
    webpack(7)-生产环境
    webpack(6)-模块热替代&tree shaking
  • 原文地址:https://www.cnblogs.com/Guhongying/p/9041380.html
Copyright © 2011-2022 走看看