Created
April 20, 2012 19:34
-
-
Save fmstefanini/2431215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Criar um programa que perco1rra uma matriz e | |
a preencha, a partir do número 1, de forma | |
espiral. A entrada do programa será o número | |
de linhas e o número de colunas da matriz. | |
Este problema é mais fácil de ser | |
compreendido através de exemplos: | |
Entrada: 4 3 | |
Saída: | |
1 2 3 | |
10 11 4 | |
9 12 5 | |
8 7 6 | |
Entrada: 6 5 | |
Saída: | |
1 2 3 4 5 | |
18 19 20 21 6 | |
17 28 29 22 7 | |
16 27 30 23 8 | |
15 26 25 24 9 | |
14 13 12 11 10*/ | |
import java.util.Scanner; | |
public class MatrizEspiral{ | |
public static void main(String args[]){ | |
int linha,coluna; | |
Scanner nome = new Scanner (System.in); | |
System.out.println ("Entre com o numero de linhas:"); | |
linha = nome.nextInt(); | |
System.out.println ("Entre com o numero de colunas:"); | |
coluna = nome.nextInt(); | |
int x[][] = new int[linha][coluna]; | |
int i = 0, indice = 0, j = 0; | |
for(i = 0; i < coluna; i++){ | |
indice++; | |
x[0][i] = indice; | |
//System.out.print(x[0][i]+" "); | |
//for ( int j = i | |
} | |
for(j = 1; j < linha; j++) { | |
indice++; | |
x[j][coluna - 1] = indice; | |
//System.out.println (x[j][coluna - 1]); | |
} | |
for(i = coluna - 1; i >=0; i--){ | |
x[j-1][i] = indice; | |
indice++; | |
} | |
for (j = linha - 2; j>0; j--){ | |
x[j][i+1] = indice; | |
indice++; | |
} | |
Impressao(x, coluna, linha); | |
} | |
public static void Impressao(int [][] m, int c, int l) { | |
//m[][] = new int[c][l]; | |
for (int i = 0; i<l; i++){ | |
//System.out.println(); | |
for(int j = 0; j<c; j++){ | |
System.out.print(m[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment