Java - QuIeN Es CaPaZ dE AyUdArMe

 
Vista:

QuIeN Es CaPaZ dE AyUdArMe

Publicado por Andres Felipe Francoe (39 intervenciones) el 06/03/2007 03:41:36
tengo que hacer esto en java

me dan una matriz asi.....

9 8 5 y me tiene que quedar organizada..asi 0 1 2
0 6 1 4 5 6
4 7 2 7 8 9


esa es la idea..una matriz cuadrada..que quede organizada..asi.......y quien me puede ayudar...

POR FAVOR..YA LO HE INTENTADO PERO NO ME DIO...QUIEN ME AYUDA
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:QuIeN Es CaPaZ dE AyUdArMe

Publicado por Alvaro (4 intervenciones) el 06/03/2007 14:05:46
//En esta Clase que llamé Matriz2D tienes un método que se llama //ordenarMatrizAscendente (el último)que te ordena y visualiza una matriz cuadrada que //tiene todos sus elementos distintos. Es Suerte. Saludos

public class Matriz2D {
private double m2D[][];
private int nF=0;
private int nC=0;

public Matriz2D() {
}

public Matriz2D(int numFilas,int numCol) {
m2D=new double[numFilas][numCol];
nF=numFilas;
nC=numCol;
}

public int getNumFila(){
return nF;
}
public int getNumCol(){
return nC;
}

public void setValor(int Fila,int Col,double valor){
m2D[(Fila-1)][(Col-1)]=valor;//se cuenta al meter desde el uno
// y no desde el cero
this.sonTodosDistintos();
}

public double getValor(int Fila,int Col){
return m2D[Fila][Col];
}

public void visualizaMatriz(){
System.out.print("La matriz contiene: ");
for (int i=0; i<this.getNumFila();i++){
System.out.println(" ");
for(int j=0; j<this.getNumCol();j++){
System.out.print(m2D[i][j]+" ");
}
}
System.out.println(" ");
}

public void sonTodosDistintos(){
boolean distinto = true;
double valor = -1;
for (int i = 0; i < this.getNumFila(); i++) {
for (int j = 0; j < this.getNumCol(); j++) {
valor = m2D[i][j];
for (int k = 0; k < this.getNumFila(); k++) {
for (int l = 0; l < this.getNumCol(); l++) {
if (i == k && j == l) {
}
else {
if (valor == m2D[k][l]) {
System.out.println("El valor " + m2D[k][l] +
" está repetido en pos(" + (k + 1) + "," +
(l + 1) + ")");
distinto = false;
break;
}
else {
//System.out.print(m2D[i][j]+" ");
}
}
}
}
}
}
if (distinto) {
System.out.print("La matriz tiene todos sus elementos distintos \n");
}
}

public boolean sonTodosDistintos2(){
boolean distinto = true;
double valor = -1;
for (int i = 0; i < this.getNumFila(); i++) {
for (int j = 0; j < this.getNumCol(); j++) {
valor = m2D[i][j];
for (int k = 0; k < this.getNumFila(); k++) {
for (int l = 0; l < this.getNumCol(); l++) {
if (i == k && j == l) {
}
else {
if (valor == m2D[k][l]) {
System.out.println("El valor " + m2D[k][l] +
" está repetido en pos(" + (k + 1) + "," +
(l + 1) + ")");
distinto = false;
break;
}
else {
//System.out.print(m2D[i][j]+" ");
}
}
}
}
}
}
if(distinto){
//System.out.print("La matriz tiene todos sus elementos distintos \n");
return true;
}
else {
return false;
}
//return distinto;
}

public void buscaPtoSilla(){
boolean pSilla = false;
boolean menorFila = false;
double valorAux=-1;
double menorF=-1;
int filaPSilla=-1;
int colPSilla=-1;
for (int i=0; i<this.getNumFila();i++){
for (int j = 0; j < this.getNumCol(); j++) {
valorAux = m2D[i][j];
for (int k=0; k<this.getNumCol();k++){
if (k==j){
}
else{
if(valorAux<m2D[i][k]){
menorFila=true;
menorF=valorAux;
}
else{
menorFila=false;
break;
}
}
}
if(menorFila){
for(int l=0;l<this.getNumFila();l++){
if(l==i){
}
else{
if (valorAux > m2D[l][j]) {
pSilla = true;
}
else {
pSilla = false;
break;
}
}
}
if(pSilla){
System.out.println("Hay un ptoSilla en la pos(" + i + "," + j +
") y vale: " + m2D[i][j]);

}

}
}
}
}

public void hayAlgunCero(){
boolean cero=false;
for (int i=0; i<this.getNumFila();i++){
for (int j = 0; j < this.getNumCol(); j++) {
if(m2D[i][j]==0){
cero=true;
System.out.println("Hay un cero en ("+i+","+j+")");
}
}
}
}


public void ordenarMatrizAsc(){
double temp=-1;
if(this.getNumFila()==this.getNumCol()){
if (this.sonTodosDistintos2() == true) {
System.out.println("No todos los elementos son distintos");
}
else {
for(int i=0; i<this.getNumFila();i++){
for(int j=0;j<this.getNumCol();j++){
for(int k=0;k<this.getNumFila();k++){
for(int l=0;l<this.getNumCol();l++){
if(i==k&&j==l){}
else{
if (m2D[i][j] < m2D[k][l]){
}
else{
temp=m2D[i][j];
m2D[i][j]=m2D[k][l];
m2D[k][l]=temp;
}
}
}
}
}
}
}
}
else {
System.out.println("La matriz no es cuadrada");
}

this.visualizaMatriz();
}

}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar