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

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main()
    {
        int n;
        int a[11][11];
        while(~scanf("%d",&n)){
            memset(a,0,sizeof(a));
            int tot = 1;
            int x,y;
            a[0][0] = 1;
            x = 0;y = 0;
            while(tot < n*n){
               while(y < n - 1 && !a[x][y+1]) a[x][++y] = ++tot; 
               while(x < n - 1 && !a[x+1][y]) a[++x][y] = ++tot;
               while(y >= 1 && !a[x][y-1]) a[x][--y] = ++tot;
               while(x >= 1 && !a[x-1][y]) a[--x][y] = ++tot;
            }
            if(n <= 3 ){
            for(x = 0 ; x < n ; x++){
                for(y = 0 ; y < n;y++){
                    printf("%d ",a[x][y]);
                }
                printf("
    ");
               }
            }
            else if(n >= 4 && n <= 9){
                for(x = 0 ; x < n ; x++){
                  for(y = 0 ; y < n ; y++)
                    printf("%2d ",a[x][y]);
                    
                    printf("
    ");
               }
            }
            else {
                for(x = 0 ; x < n ; x++){
                    for(y = 0 ; y < n ; y++){
                   if( x == 5 && y == 3)
                    printf("%2d ",a[x][y]);
                   else 
                    printf("%2d  ",a[x][y]);
                    }
                    printf("
    ");
                }
            }
                    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Windows 编程
    javascript 快速入门
    python 在windows下监听键盘按键
    eleme 项目使用到的库
    lftp 快速使用
    自动测试工具
    Cookie(1)
    fillder script使用
    Boost.Hana
    Haskell语言学习笔记(50)Extra
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4442534.html
Copyright © 2011-2022 走看看