gpt4 book ai didi

java - 矩阵交换行 - Java

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:37 30 4
gpt4 key购买 nike

我需要编写一段代码来加载方阵,然后将第一行与第一列中元素最大的行交换,然后打印交换的数组。

我可以获得第一列的最大元素,但我不知道如何进行交换。

import java.io.*;
import java.util.*;
import static java.lang.System.out;

class Matriz4_Metodos{

static String cadena;
static InputStreamReader entrada = new InputStreamReader(System.in);
static BufferedReader recibedatos = new BufferedReader(entrada);

public static void main (String[] args) throws java.io.IOException {
int n,mayor=0;

n=LeerNumero("Ingresa el numero de los renglones y columnas:");

int Matriz[][] = new int [n][n];

Matriz=CargarMatriz(Matriz);

ImprimirMatriz(Matriz);

out.println("\nEl mayor de la primera columna es:"+EncontrarMayor(Matriz,mayor));

}

public static int LeerNumero (String msgtxt) throws java.io.IOException{

do{
int xnm;
out.print(msgtxt);
cadena=recibedatos.readLine();
try{
xnm=Integer.parseInt(cadena);
if (xnm<0){
out.println("Solo positivos");
continue;
}
return xnm;
}
catch (NumberFormatException e){
out.println("Numero de dato incorrecto");
}
}while(true);

}

public static int[][] CargarMatriz (int xMatriz[][]) throws java.io.IOException{
Random Aleatorio =new Random();
int nal;
for(int i=0; i<xMatriz.length; i++){
for(int j=0; j<xMatriz[0].length; j++){
nal=Aleatorio.nextInt(10);
xMatriz[i][j]=nal;
}

}

return xMatriz;
}

public static void ImprimirMatriz (int xMatriz[][]){
out.println("\n");
for(int i=0; i<xMatriz.length; i++){
for(int j=0; j<xMatriz[0].length; j++){
out.print(xMatriz[i][j]+" ");
}
out.println("\n");
}
}

public static int EncontrarMayor (int xMatriz[][],int xmayor) throws java.io.IOException {
xmayor=0;

for(int i=0; i<xMatriz.length; i++){
for(int j=0; j<xMatriz[0].length; j++){
if(j==0){
if(xMatriz[i][j]>xmayor){
xmayor=xMatriz[i][j];
}
}
}
}
return xmayor;
}

}

最佳答案

在上一个问题 Matrix Swapping Columns - Java 中找到答案交换列。

这是代码。 如需更多说明,请阅读内嵌评论。

    int[][] arr = new int[][] { { 1, 4, 5, 4 }, { 7, 6, 4, 2 }, { 1, 1, 2, 3 }, { 1, 2, 2, 5 } };

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

// logic begins here
int rowWithHighestValueInFirstColumn = 1;
for (int i = 0; i < arr[rowWithHighestValueInFirstColumn].length; i++) {
// store the value of highest row
int temp = arr[rowWithHighestValueInFirstColumn][i];
// swap the value of highest row with first row
arr[rowWithHighestValueInFirstColumn][i] = arr[0][i];
// set the value of first row that is stored in temp
arr[0][i] = temp;
}
// logic ends here

System.out.println("After swapping");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

输出:(第二行与第一行交换)

    1   4   5   4   
7 6 4 2
1 1 2 3
1 2 2 5
After swapping
7 6 4 2
1 4 5 4
1 1 2 3
1 2 2 5

关于java - 矩阵交换行 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038325/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com