• 【ccf- csp201412-2】z字形扫描


     1 //ccf-Z字形扫描
     2 #include<iostream>
     3 #include<cmath>
     4 using namespace std;
     5 #define N 500
     6 int main()
     7 {
     8     unsigned n, i, j;cin >> n;
     9     //n = 4;
    10     //unsigned a[N][N] = { { 1, 5, 3, 9 },{ 3, 7 ,5, 6 },{ 9, 4, 6, 4 },{ 7 ,3, 1, 3 } };
    11     unsigned a[N][N];
    12     for (i = 0; i < n; i++)
    13         for (j = 0; j < n; j++)
    14             cin >> a[i][j];
    15 
    16     i = 0; j = 0;
    17     int k = 0, m;
    18     cout << a[0][0] << ' ';
    19     while (k <= 2 * n - 2)
    20     {
    21         ++k;
    22         if (k < n)
    23         {
    24             if (k % 2)
    25             {
    26                 j++;
    27                 i = 0;
    28                 while (i < k)
    29                 {
    30                     cout << a[i][j] << ' ';
    31                     j--;
    32                     i++;
    33                 }
    34             }
    35             else
    36             {
    37                 i++;
    38                 //j = 0;
    39                 while (j < k)
    40                 {
    41                     cout << a[i][j] << ' ';
    42                     i--;
    43                     j++;
    44                 }
    45             }
    46             cout << a[i][j] << ' ';
    47         }
    48         else
    49         {
    50             if (k % 2)
    51             {
    52                 m = 2 * n - k;
    53                 //j = n - 1;
    54                 //i++;
    55                 j = n;
    56                 while (--m)
    57                 {
    58                     
    59                     i++;
    60                     j--;
    61                     cout << a[i][j] << ' ';
    62                 }
    63             }
    64             else
    65             {
    66                 m = 2 * n - k;
    67                 i = n;
    68                 while (--m)
    69                 {
    70                     j++;
    71                     i--;
    72                     cout << a[i][j] << ' ';
    73                 }
    74             }
    75         }
    76     }
    77     return 0;
    78 }

     注意i,j输出后的值即可~

  • 相关阅读:
    TO DO List
    springboot 热部署
    <dependencyManagement>的作用
    人体工程学座椅
    temp
    temp
    声明式编程和命令式编程的本质区别
    weak first question
    Spring依赖注入方式和依赖来源
    SpringBoot 整合 H2 数据库
  • 原文地址:https://www.cnblogs.com/yuelien/p/6369016.html
Copyright © 2020-2023  润新知