zoukankan      html  css  js  c++  java
  • 基础练习 回形取数

    问题描述
      回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
    输入格式
      输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
    输出格式
      输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。
    样例输入
    3 3
    1 2 3
    4 5 6
    7 8 9
    样例输出
    1 4 7 8 9 6 3 2 5
    样例输入
    3 2
    1 2
    3 4
    5 6
    样例输出
    1 3 5 6 4 2
     
    哇,发现自己很久没码代码了。。。然后这个题目想了很久还是不会做啊,很难受。。。其实写出来之后,发现好像也不难写啊。。。
    蒻啊
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 #include<algorithm>
     6 using namespace std;
     7 int a[250][250];
     8 
     9 int main(){
    10     int n, m;
    11    // memset(vis, 0, sizeof(vis));
    12     memset(a, -1, sizeof(a));
    13     cin >> n >> m;
    14     for(int i = 0; i < n; i++){
    15         for(int j = 0; j < m; j++){
    16             scanf("%d", &a[i][j]);
    17         }
    18     }
    19     int x = -1, y = 0, cnt = 0;
    20     while(cnt < n * m){
    21         while(x + 1 < n && a[x + 1][y] != -1){
    22             printf("%d ", a[++x][y]);
    23             a[x][y] = -1;
    24             cnt++;
    25         }
    26         while(y + 1 < m && a[x][y + 1] != -1){
    27             printf("%d ", a[x][++y]);
    28             a[x][y] = -1;
    29             cnt++;
    30         }
    31         while(x  - 1 >= 0 && a[x - 1][y] != -1){
    32             printf("%d ", a[--x][y]);
    33             a[x][y] = -1;
    34             cnt++;
    35         }
    36         while(y - 1 >= 0 && a[x][y - 1] != -1){
    37             printf("%d ", a[x][--y]);
    38             a[x][y] = -1;
    39             cnt++;
    40         }
    41     }
    42     cout << endl;
    43     return 0;
    44 }
  • 相关阅读:
    CSS3 target伪类简介
    不用position,让div垂直居中
    css3 在线编辑工具 连兼容都写好了
    a标签伪类的顺序
    oncopy和onpaste
    【leetcode】1523. Count Odd Numbers in an Interval Range
    【leetcode】1518. Water Bottles
    【leetcode】1514. Path with Maximum Probability
    【leetcode】1513. Number of Substrings With Only 1s
    【leetcode】1512. Number of Good Pairs
  • 原文地址:https://www.cnblogs.com/ledoc/p/7044124.html
Copyright © 2011-2022 走看看