No triângulo de Pascal, o valor de cada linha é a soma dos valores acima da linha anterior. Se nenhum valor estiver disponível, um é usado. Usando a notação Java n [k] = n-1 [k-1] + n-1 [k]. A primeira linha contém o valor um.
O triângulo de Pascal é gerado usando 2 loops e começando no topo com o primeiro elemento.
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class InterviewPascalTriangle {
protected List<List<Integer>> generatePascalTriangle(int nbRows){
// Validação de entrada
if ( nbRows < 0 )
throw new IllegalArgumentException( "O número de linhas não pode ser negativo." );
// O objeto a devolver
List<List<Integer>> toReturn = new LinkedList<List<Integer>>();
// Salve a linha anterior.
List<Integer> previousRow = Collections.emptyList();
// Processe todas as linhas
for( int i = 0 ; i <= nbRows ; i++ ){
// Crie a linha atual
List<Integer> currentRow = new LinkedList<Integer>();
toReturn.add( currentRow );
// Definir o valor anterior
int previousValue = 0;
// Loop em todos os valores
for(int c : previousRow){
currentRow.add( c + previousValue );
// Definir o valor atual como o anterior
previousValue = c;
}
// Adicione o último valor
currentRow.add( 1 );
// Salve a linha atual.
previousRow = currentRow;
}
return toReturn;
}
public static void main(String[] argv) throws Exception {
InterviewPascalTriangle t = new InterviewPascalTriangle();
List<List<Integer>> res = t.generatePascalTriangle(7);
System.out.println( "Resultado:" );
String tmp = res.get( res.size() -1 ).toString();
int max = (int) tmp.length();
for(List<Integer> c : res ){
String toDisplay = c.toString();
int padding = ( max - toDisplay.length()) / 2 ;
StringBuilder sb = new StringBuilder();
for(int i=0; i < padding; i++){
sb.append( " " );
}
sb.append( toDisplay );
System.out.println( sb.toString() );
}
}
}
Resultado:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
As linhas do triângulo de Pascal são numeradas de 0 a n. A primeira linha contém apenas o valor 1.