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

    1160 蛇形矩阵

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 白银 Silver
     
     
     
    题目描述 Description

    小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1开始以逆时针方向绕行,逐圈扩大,直到n行n列填满数字,请输出该n行n列正方形矩阵以及其的对角线数字之和.

    输入描述 Input Description

    n(即n行n列)

    输出描述 Output Description

    n+1行,n行为组成的矩阵,最后一行为对角线数字之和

    样例输入 Sample Input

    3

    样例输出 Sample Output

    5 4 3
    6 1 2
    7 8 9
    25

     1 #include<iostream>
     2 using namespace std;
     3 int now=1;
     4 int a[101][101];
     5 int s=0;
     6 int fx=1;// 1右 2左 3上 4下 
     7 int tot=1;
     8 int ans=0;
     9 int main()
    10 {
    11     int n;
    12     cin>>n;
    13     s=n/2+1;
    14     int i=s;
    15     int j=s;
    16     a[i][j]=now;
    17     now++;
    18     while(tot!=n*n)
    19     {
    20         if(fx==1&&j-i==1)
    21         {
    22             fx=3;
    23             //a[i][j]=now;
    24             //now++;
    25         //    tot++;
    26         }
    27         if(fx==2&&i==j)
    28         {
    29             fx=4;
    30             //a[i][j]=now;
    31         //    now++;
    32         //    tot++;
    33         }
    34         if(fx==3&&(i+j==n+1))
    35         {
    36             fx=2;
    37             //a[i][j]=now;
    38         //    now++;
    39         //    tot++;
    40         }
    41         if(fx==4&&(i+j==n+1))
    42         {
    43             fx=1;
    44             //a[i][j]=now;
    45         //    now++;
    46         //    tot++;
    47         }
    48         if(fx==1)// 1右 2左 3上 4下 
    49         {
    50             j++;
    51             a[i][j]=now;
    52             now++;
    53             tot++;
    54         }
    55         if(fx==2)// 1右 2左 3上 4下 
    56         {
    57             j--;
    58             a[i][j]=now;
    59             now++;
    60             tot++;
    61         }
    62         if(fx==3)// 1右 2左 3上 4下 
    63         {
    64             i--;
    65             a[i][j]=now;
    66             now++;
    67             tot++;
    68         }
    69         if(fx==4)// 1右 2左 3上 4下 
    70         {
    71             i++;
    72             a[i][j]=now;
    73             now++;
    74             tot++;
    75         }
    76     }
    77     for(int i=1;i<=n;i++)
    78     {
    79         for(int j=1;j<=n;j++)
    80         {
    81             cout<<a[i][j]<<" ";
    82             if((i+j==n+1)||(i==j))
    83             ans=ans+a[i][j];
    84         }
    85         cout<<endl;
    86     }
    87     cout<<ans;
    88     return 0;
    89 }
  • 相关阅读:
    递归算法的时间复杂度分析
    MongoDB入门简单介绍
    关于用例须要多少文档以及业务用例等等
    Java连接redis的使用演示样例
    C++ String 转 char*
    MySQL和PostgreSQL 导入数据对照
    SSL连接建立过程分析(1)
    XTU OJ 1210 Happy Number (暴力+打表)
    Codeforces Round #258 (Div. 2)[ABCD]
    CreateFont具体解释
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6575763.html
Copyright © 2011-2022 走看看