矩阵行列变换

矩阵交换

http://www.nowcoder.com/questionTerminal/ec44d4ff8c794b2f9205bdddbde96817

import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        int n = Integer.parseInt(str[0]), m = Integer.parseInt(str[1]);
        String[][] v = new String[n][m];
        for(int i = 0; i < n; ++i){
            String[] s = sc.nextLine().split(" ");
            for(int j = 0; j < m; ++j) v[i][j] = s[j];
        }
        int k = Integer.parseInt(sc.nextLine());
        for(int i = 0; i < k; ++i){
            String[] s = sc.nextLine().split(" ");
            int a = Integer.parseInt(s[1]) - 1, b = Integer.parseInt(s[2]) - 1;
            if(s[0].equals("r")){
                for(int j = 0; j < m; ++j){
                    String temp = v[a][j];
                    v[a][j] = v[b][j];
                    v[b][j] = temp;
                }
            }
            if(s[0].equals("c")){
                for(int j = 0; j < n; ++j){
                    String temp = v[j][a];
                    v[j][a] = v[j][b];
                    v[j][b] = temp;
                }
            }
        }
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < m; ++j){
                System.out.printf("%s ", v[i][j]);
            }
            System.out.println();
        }
    }
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务