【问题标题】:Swap two columns of a matrix交换矩阵的两列
【发布时间】:2019-07-03 14:22:39
【问题描述】:

我给出了一个二维数组(矩阵)和两个数字:i 和 j。我的目标是用矩阵中的索引 i 和 j 交换列。输入包含矩阵维度 n 和 m,不超过 100,然后是矩阵的元素,然后是索引 i 和 j。

我猜问题的根源与引用的变量有关?我试图用

替换第15行
int nextValue = scanner.nextInt();
matrix[i][j] = nextValue;
swap[i][j] = nextValue;

但输出仍然保持不变......

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int column = scanner.nextInt();

        int[][] matrix = new int[row][column];
        int[][] swap = matrix.clone();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int c0 = scanner.nextInt();
        int c1 = scanner.nextInt();

        for (int i = 0; i < row; i++) {
            swap[i][c0] = matrix[i][c1];
            swap[i][c1] = matrix[i][c0];
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(swap[i][j] + " ");
            }
            System.out.println();
        }

    }
}

我的意见:

3 4 
11 12 13 14 
21 22 23 24 
31 32 33 34 
0 1

3 和 4 代表矩阵的行数和列数,以下三行定义矩阵的元素,最后一行告诉程序交换哪些列。

预期输出:

12 11 13 14 
22 21 23 24 
32 31 33 34 

实际输出:

12 12 13 14 
22 22 23 24 
32 32 33 34

【问题讨论】:

标签: java


【解决方案1】:

在您的程序中,逻辑错误是它们: 查看我的代码

import javax.sound.sampled.Line;
import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // put your code here
        int i = scanner.nextInt();
        int j = scanner.nextInt();
        int[][] matrix = new int[i][j];
        for (int y = 0; y < i; y++) {
            for (int x = 0; x < j; x++) {
                matrix[y][x] = scanner.nextInt();
            }
        }
        int swap1 = scanner.nextInt();
        int swap2 = scanner.nextInt();
        for (int[] arr : matrix) {
            int temp = arr[swap1];
            arr[swap1] = arr[swap2];
            arr[swap2] = temp;
            for (int el : arr) {
                System.out.print(el + " ");
            }
            System.out.println();
        }
    }
}

【讨论】:

    【解决方案2】:

    非常感谢@Tim Biegeleisen!

    这段代码对我有用:

    import java.util.Scanner;
    
    public class Main {
    
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int column = scanner.nextInt();
    
        int [][] matrix = new int[row][column];
        int [][] swap = matrix.clone();
    
        for (int i = 0; i < row; i++) {
          for (int j = 0; j < column; j++) {
            matrix[i][j] = scanner.nextInt();}}
    
        int c0 = scanner.nextInt();
        int c1 = scanner.nextInt();
    
        for (int i=0; i < row; i++) {
          int temp = matrix[i][c0];
          matrix[i][c0] = matrix[i][c1];
          matrix[i][c1] = temp;
        }
    
        for (int i = 0; i < row; i++) {
          for (int j = 0; j < column; j++) {
            System.out.print(swap[i][j]+" "); }
          System.out.println();}
    
      }
    }
    

    【讨论】:

    • 我的回答是否有问题导致您无法接受?
    【解决方案3】:

    您的交换逻辑似乎已关闭。如果你想交换两个变量,比如ab,那么这里有一个适用于 Java 的模式:

    int a = 5;
    int b = 10;
    int temp = a;
    a = b;
    b = temp;
    

    将此逻辑应用于矩阵列交换,我们可以尝试以下更新的代码:

    int c0 = scanner.nextInt();
    int c1 = scanner.nextInt();
    
    for (int i=0; i < row; i++) {
        int temp = matrix[i][c0];
        matrix[i][c0] = matrix[i][c1];
        matrix[i][c1] = temp;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2021-04-29
      • 2019-03-24
      • 1970-01-01
      • 2013-08-01
      • 2013-09-14
      • 2012-12-26
      相关资源
      最近更新 更多