• 蓝桥杯 基础练习 BASIC-25 回形取数


    基础练习 回形取数  
    时间限制:1.0s   内存限制:512.0MB
    问题描述
      回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转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 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 public class Main {
     6     public static void main(String[] args) throws IOException {
     7         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     8         String[] str = br.readLine().split(" ");
     9         int n = Integer.parseInt(str[0]);
    10         int m = Integer.parseInt(str[1]);
    11         
    12         int[][] num = new int[n][m];
    13         for(int i = 0; i < n; i++){
    14             String[] t = br.readLine().split(" "); 
    15             for(int j = 0; j < m; j++){
    16                 num[i][j] = Integer.parseInt(t[j]);
    17             }
    18         }
    19         
    20         int[] memory = new int[n*m];        //存储每一步取到的数
    21         loopGetNum(n,m,num,memory);
    22         
    23         for(int i = 0; i < memory.length; i++){
    24             System.out.print(memory[i]+" ");
    25         }
    26         
    27     }
    28 
    29     private static void loopGetNum(int n, int m, int[][] num, int[] memory) {
    30         int row = 0;          //
    31         int col = 0;          //
    32         int cirNum = 0;       //圈数
    33         int total = n * m;    //总的执行次数,即数字的总个数
    34         
    35         for(int i = 0; i < total; i++){
    36             if(row < n && col == cirNum){                //第一列
    37                 memory[i] = num[row][col];
    38                 row++;
    39             }else if(row == n && col < m-1){            //最后一行
    40                 col++;
    41                 memory[i] = num[row-1][col];
    42             }else if(row-1 > cirNum && col == m-1){        //最后一列
    43                 row--;
    44                 memory[i] = num[row-1][col];
    45             }else if(row-1 == cirNum && col > cirNum){    //第一行
    46                 col--;
    47                 memory[i] = num[row-1][col];
    48                 if(row-1 == cirNum && col == cirNum+1){    //若到第一行第二个数字,则缩小矩阵行列范围,形成子矩阵
    49                     cirNum++;
    50                     n = n-1;
    51                     m = m-1;
    52                     row = cirNum;
    53                     col = cirNum;
    54                 }
    55                 
    56             }
    57         }
    58     }
    59 }
  • 相关阅读:
    大话设计模式:外观模式
    大话设计模式:零篇-目录总结
    大话设计模式:观察者模式
    Spring MVC自动为对象注入枚举数据
    使用idea工具开发webservice
    HTTP协议详解
    mysql 数据库 exists 和count
    eclipse运行maven的jetty插件内存溢出
    400,404,500报错页面总结
    Mac系统下Eclipse代码联想功能(代码助手,代码提示)快捷键
  • 原文地址:https://www.cnblogs.com/cao-lei/p/6686319.html
Copyright © 2020-2023  润新知