zoukankan      html  css  js  c++  java
  • [Jobdu] 题目1391:顺时针打印矩阵

    题目描述:

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:

    1 2 3 4

    5 6 7 8

    9 10 11 12

    13 14 15 16

    则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    输入:

    输入可能包含多个测试样例,对于每个测试案例,

    输入的第一行包括两个整数m和n(1<=m,n<=1000):表示矩阵的维数为m行n列。

    接下来的m行,每行包括n个整数,表示矩阵的元素,其中每个元素a的取值范围为(1<=a<=10000)。 

    输出:

    对应每个测试案例,输出一行,

    按照从外向里以顺时针的顺序依次打印出每一个数字,每个数字后面都有一个空格。

    样例输入:
    4 4
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
    
    样例输出:
    1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 

    没有算法,一圈一圈的处理,要注意的是只有一行或一列的情况。

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4  
     5 int a[1000][1000];
     6  
     7 void print(int x, int y, int m, int n) 
     8 {
     9     for (int i = y; i < y + n; i++) {
    10         cout << a[x][i] << " ";
    11     }
    12     for (int i = x + 1; i < x + m; i++) {
    13         cout << a[i][y+n-1] << " ";
    14     }
    15     if(m!=1){
    16         for (int i = y + n - 2; i >= y; i--) {
    17             cout << a[x+m-1][i] << " ";
    18         }
    19     }
    20     if(n!=1){
    21         for (int i = x + m - 2; i > x; i--) {
    22             cout << a[i][y] << " ";
    23         }
    24     }
    25 }
    26  
    27 int main()
    28 {
    29     //freopen("input.txt", "r", stdin);
    30     int m, n;
    31     while (cin >> m >> n) {
    32         for (int i = 0; i < m; i++) {
    33             for (int j = 0; j < n; j++) {
    34                 cin >> a[i][j];
    35             }
    36         }
    37         int x = 0, y = 0;
    38         while (m > 0 && n > 0) {
    39             print(x, y, m, n);
    40             x++;
    41             y++;
    42             m -= 2;
    43             n -= 2;
    44         }
    45         cout << endl;
    46     }
    47     return 0;
    48 }
    49  
    50 /**************************************************************
    51     Problem: 1391
    52     User: hupo250
    53     Language: C++
    54     Result: Accepted
    55     Time:900 ms
    56     Memory:5424 kb
    57 ****************************************************************/
  • 相关阅读:
    es6的常用方法
    axios的常用方法
    前端web初级面试总结 简述web与w3c标准的认识
    h5--手写svg动态饼图
    h5--本地存储 sessionStorage,localstorage
    vue学习---生命周期钩子activated,deactivated
    h5学习 -- 拖拽事件模拟垃圾桶
    React-redux
    React-router-dom 路由
    es6 字符串
  • 原文地址:https://www.cnblogs.com/easonliu/p/3645356.html
Copyright © 2011-2022 走看看