题解 | #C翻转#

C翻转

https://www.nowcoder.com/practice/a591300637874f6ba2316a5fe3e94579


import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void swapMatrix(int[][] temp) {
        int len = temp.length;
        int low = 0, high = len - 1;
        while (low < high) {
            int[] t1 = Arrays.copyOf(temp[low], len);
            System.arraycopy(temp[high], 0, temp[low], 0, len);
            System.arraycopy(t1, 0, temp[high], 0, len);
            low++;
            high--;
        }


        for (int i = 0; i < len; i++) {
            for (int j = 0; j <= i; j++) {
                int k = temp[i][j];
                temp[i][j] = temp[j][i];
                temp[j][i] = k;
            }
        }
    }
    public  static void matrixReverse(int[][] matrix, int startRow, int endRow,
                                      int startCOl, int endCol, boolean flag) {
        int len = endRow - startRow + 1;
        int[][] temp = new int[len][len];
        if (flag) {
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    temp[i][j] = matrix[i + startRow][j + startCOl];
                }
            }

            swapMatrix(temp);

            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    matrix[i + startRow][j + startCOl] = temp[i][j];
                }
            }
        } else {
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    temp[i][j] = matrix[j + startRow][i + startCOl];
                }
            }
            swapMatrix(temp);
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    matrix[startRow + j][startCOl + i] = temp[i][j];
                }
            }

        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int[][] matrix = new int[5][5];
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    matrix[i][j] = scanner.nextInt();
                }
            }
            int op1 = scanner.nextInt();
            int op2 = scanner.nextInt();
            int startRow = scanner.nextInt() - 1;
            int startCol = scanner.nextInt() - 1;
            matrixReverse(matrix, startRow,
                          (op2 == 2 ? startRow + 1 : startRow + 2),
                          startCol,
                          (op2 == 2 ? startCol + 1 : startCol + 2),
                          op1 == 1
                         );

            for (int i = 0; i < 5; i++) {

                for (int j = 0; j < 5; j++) {

                    System.out.print(matrix[i][j] + " ");

                }
                System.out.println();
            }

        }
    }
}

全部评论

相关推荐

Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务