Jump to content

For na diagonal em java


mZ
 Share

Recommended Posts

sim é uma matriz quadrada... mas o objectivo era ele percorrer todas as diagonais possíveis, começar numa ponta e acabar noutra... assim ele vai só à diagonal do meio...

o array tem este aspecto:

http://img507.imageshack.us/img507/7710/arraytg8.jpg

e o que é pedido é passar naquelas linhas de cor na diagonal...e depois ao contrário mas isso e mudar valores...

tks  🙂

Link to comment
Share on other sites

Se bem percebi o problema, penso que seja isto (a não ser que me tenha escapado alguma burrice minha  ?):

int[][] array = {{0, 1, 2}, {3, 4, 5},{6, 7, 8}};

int coluna = 1;
int linha = 0;

// testes: limites inferiores, superiores e se ou linha == 0 ou coluna == 0 (para ter uma diagonal valida)

int fim;

if (linha != 0)
fim = array.length - linha;
else 
fim = array.length - coluna;

for(int z = 0; z < fim; z++) {
    System.out.println(array[z+linha][z+coluna]);
}
Link to comment
Share on other sites

Oi!

A ideia é tu mudares os valores das variáveis linha e coluna. Ou seja, esse ciclo faz a diagonal que começa em [linha, coluna] = [1, 0].. Funciona para as outras diagonais. Basta mudares esses valores. Por exemplo, [linha, coluna] = [2, 0], ou [linha, coluna] = [0, 1]. Era esta a minha ideia... Mas lá está, não sei é se percebi bem a questão!   😄

Edit: Penso que já percebi a situação! Queres fazer a listagem completa da matriz, mas listando diagonal a diagonal, certo?

Link to comment
Share on other sites

Este é um modo possível:

int[][] array = {{0, 1, 2, 3}, 
                 {1, 0, 1, 2},
                 {2, 1, 0, 1},
                 {3, 2, 1, 0}};

for(int i = 0 ; i < array.length ; ++i){

    System.out.print("\nDiagonal[" + i + "][0] : ");

    for(int j = 0 ; j < array.length - i ; ++j){
        System.out.print(array[j][j+i] + " ");
    }
    if(i!=0){
        System.out.print("\nDiagonal[0][" + i + "] : ");

        for(int j = 0 ; j < array.length - i ; ++j){
            System.out.print(array[j][j+i] + " ");
        }
    }
} 

Resultado:

Diagonal[0][0] : 0 0 0 0

Diagonal[1][0] : 1 1 1

Diagonal[0][1] : 1 1 1

Diagonal[2][0] : 2 2

Diagonal[0][2] : 2 2

Diagonal[3][0] : 3

Diagonal[0][3] : 3

Podes retirar um ciclo for em função do problema em questão, eu como queria mostrar na consola dos valores da diagonal teve de ser assim.

Daniel Amorim

VP for xRTML

http://www.xrtml.org http://www.realtime.co

Link to comment
Share on other sites

A mim parece-me bem  ?

Pensando de outro modo, uma matriz 4x4 tem estas posições:

(0,0) (0,1) (0,2) (0,3)

(1,0) (1,1) (1,2) (1,3)

(2,0) (2,1) (2,2) (2,3)

(3,0) (3,1) (3,2) (3,3)

Mudando só o output:

int[][] array = {{0, 1, 2, 3}, 
                 {1, 0, 1, 2},
                 {2, 1, 0, 1},
                 {3, 2, 1, 0}};

for(int i = 0 ; i < array.length ; ++i){

    System.out.print("\nDiagonal : ");

    for(int j = 0; j < array.length - i ; ++j){
        System.out.print("([" + j + "," + (j+i) + "])");
    }
    if(i!=0){
        System.out.print("\nDiagonal : ");

        for(int j = 0 ; j < array.length - i ; ++j){
            System.out.print("([" + (j+i) + "," + j + "])");
        }
    }
} 

Dá isto:

Diagonal : ([0,0])([1,1])([2,2])([3,3])

Diagonal : ([0,1])([1,2])([2,3])

Diagonal : ([1,0])([2,1])([3,2])

Diagonal : ([0,2])([1,3])

Diagonal : ([2,0])([3,1])

Diagonal : ([0,3])

Diagonal : ([3,0])

Vê lá se não é o que querias 😄

Daniel Amorim

VP for xRTML

http://www.xrtml.org http://www.realtime.co

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.