1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Escreva um algoritmo que, dado n, gera e escreve o Triângulo de Pascal de ordem n, utilizando uma matriz.
O código abaixo foi transcrito em JAVA:
public class TrianguloPascal {
public static void main(String[] args) {
int nfilas = 10;
int[] a = new int[1];
for (int i = 1; i <= nfilas; i++) {
int[] x = new int[i];
for (int j = 0; j < i; j++) {
if (j == 0 || j == (i - 1)) {
x[j] = 1;
} else {
x[j] = a[j] + a[j - 1];
}
System.out.print(x[j] + " ");
}
a = x;
System.out.println();
}
}
}
O código abaixo foi transcrito em JAVA:
public class TrianguloPascal {
public static void main(String[] args) {
int nfilas = 10;
int[] a = new int[1];
for (int i = 1; i <= nfilas; i++) {
int[] x = new int[i];
for (int j = 0; j < i; j++) {
if (j == 0 || j == (i - 1)) {
x[j] = 1;
} else {
x[j] = a[j] + a[j - 1];
}
System.out.print(x[j] + " ");
}
a = x;
System.out.println();
}
}
}
Para escrever sua resposta aqui, entre ou crie uma conta.
Compartilhar