zoukankan      html  css  js  c++  java
  • 翻转数据

    该题是要翻转数据。首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。 

    操作类型有四种: 

    1 2 表示:90度,顺时针,翻转4个数 

    1 3 表示:90度,顺时针,翻转9个数 

    2 2 表示:90度,逆时针,翻转4个数 

    2 3 表示:90度,逆时针,翻转9个数 

    Sample: 

    Input: 

    1 2 3 4 5 

    6 7 8 9 10 

    11 12 13 14 15 

    16 17 18 19 20 

    21 22 23 24 25 

    1 3 1 1 

    Output: 

    11 6 1 4 5 

    12 7 2 9 10 

    13 8 3 14 15 

    16 17 18 19 20 

    21 22 23 24 25 

    #include<iostream>
    using namespace std;
    
    void reverse(int c[5][5], int x, int y, int n, bool rev) {
            for (int i = x-1; i < n; i++) {
                for (int j = y-1; j < n; j++) {
                    if (rev == true)
                        c[j][n - i - 1] = c[i][j];
                    else
                        c[n - j - 1][i] = c[i][j];
                }
            }
        
    }
    
    int main() {
        int a[5][5];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                a[i][j]=0;
            }
        }
        int b[4];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                cin >> a[i][j];
            }
        }
        for (int j = 0; j < 4; j++) {
            cin >> b[j];
        }
        if (b[0] == 1) {
            if (b[1] == 2)
                reverse(a, b[2], b[3], 2, true);
            else if (b[1] == 3)
                reverse(a, b[2], b[3], 3, true);
            else {
                cout << "操作类型不合法,请重新输入:
    ";
                for (int j = 0; j < 4; j++) {
                    cin >> b[j];
                }
            }
        }
        else if (b[0] == 2) {
            if (b[1] == 2)
                reverse(a, b[2], b[3], 2, false);
            else if (b[1] == 3)
                reverse(a, b[2], b[3], 3, false);
            else {
                cout << "操作类型不合法,请重新输入:
    ";
                for (int j = 0; j < 4; j++) {
                    cin >> b[j];
                }
            }
        }
        else {
            cout << "操作类型不合法,请重新输入:
    ";
            for (int j = 0; j < 4; j++) {
                cin >> b[j];
            }
        }
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                cout << a[i][j];
                if (j != 4)
                    cout << " ";
            }
            cout << endl;
        }
        return 0;
    }
  • 相关阅读:
    写代码如坐禅:你是哪一类程序员
    关于鸿蒙的商业讨论
    为什么你总是“把天聊死”?
    生活不易,唯有努力
    如何用一句话激怒一名程序员?
    华为正式开源方舟编译器,开源了,它真的开源了!
    为什么HTTPS比HTTP更安全?
    《管理者必读12篇》购买方法
    程序员都在用的电脑小技巧,看一遍就学会,每天早下班一小时
    一位程序员的一天工作清单:5:30下班,5:30起床
  • 原文地址:https://www.cnblogs.com/xym4869/p/8657694.html
Copyright © 2011-2022 走看看