zoukankan      html  css  js  c++  java
  • 24:蛇形填充数组

    24:蛇形填充数组

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    用数字1,2,3,4,...,n*n这n2个数蛇形填充规模为n*n的方阵。

    蛇形填充方法为:

    对于每一条左下-右上的斜线,从左上到右下依次编号1,2,...,2n-1;按编号从小到大的顺序,将数字从小到大填入各条斜线,其中编号为奇数的从左下向右上填写,编号为偶数的从右上到左下填写。

    比如n=4时,方阵填充为如下形式:

    1  2  6  7
    3  5  8  13
    4  9  12 14
    10 11 15 16
    
    输入
    输入一个不大于10的正整数n,表示方阵的行数。
    输出
    输出该方阵,相邻两个元素之间用单个空格间隔。
    样例输入
    4
    样例输出
    1 2 6 7
    3 5 8 13
    4 9 12 14
    10 11 15 16
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 using namespace std;
     6 int tot=1;
     7 int ans=2;
     8 int now=1;// 1向下 2向上 
     9 int a[101][101];
    10 int main()
    11 {
    12     int n;
    13     cin>>n;
    14     int i=1,j=2;
    15     cout<<1<<" ";
    16     while(tot!=n*n)
    17     {
    18         if(now==1)
    19         {
    20             
    21             a[i][j]=ans;
    22             ans++;    
    23             tot++;
    24             if(j==1||i==n)
    25             {
    26                 if(j==1&&i!=n)
    27                 i++;
    28                 else j++;
    29                 now=2;
    30                 continue;
    31             }
    32             else
    33             {
    34                 i++;
    35                 j--;
    36             }
    37             
    38         }
    39         if(now==2)
    40         {
    41             
    42             a[i][j]=ans;
    43             ans++;
    44             tot++;
    45             if(i==1||j==n)
    46             {
    47                 if(i==1&&j!=n)
    48                 j++;
    49                 else i++;
    50                 now=1;
    51                 continue;
    52             }
    53             else
    54             {
    55                 i--;
    56                 j++;    
    57             }
    58         }
    59         else now=1;    
    60     }
    61     for(int i=1;i<=n;i++)
    62     {
    63         for(int j=1;j<=n;j++)
    64         {
    65             if(i==1&&j==1)continue;
    66             else
    67             cout<<a[i][j]<<" ";
    68         }
    69         cout<<endl;
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    玩具数据库
    数据库中可能会出现的表
    SqlDbHelper
    重写 覆盖 虚方法
    页面标签的初始化
    如何将UTF8转换为UTF8n
    小软件项目开发的管理(转)
    SCRUM软件开发过程(转)
    在.Net如何制作自定义的快捷方式
    What Is a Leader
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6516477.html
Copyright © 2011-2022 走看看