Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pascal;
- /**
- *
- * @author Kyl3
- */
- import java.util.Scanner;
- class Space {
- public static void p(int count) {
- for (int i = 0; i < count; i++) {
- System.out.print(" ");
- }
- }
- }
- public class Pascal {
- public static void main(String[] args) {
- System.out.println("Digite o número de linhas: ");
- int n = new Scanner(System.in).nextInt() + 1;
- System.out.println("Digite o espaçamento entre os valores: ");
- int max_dist = new Scanner(System.in).nextInt();
- if (max_dist <= 1) {
- max_dist = 2;
- }
- int mat[][] = new int[n][n];
- int maxj[] = new int[n];
- for (int i = 0; i < n; i++) {
- if (i == 0) {
- mat[i][i] = 1;
- } else {
- for (int j = 1; j < n; j++) {
- mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
- if (mat[i][j] > maxj[j]) {
- maxj[j] = mat[i][j];
- }
- }
- for (int j = i; j < n; j++) {
- mat[i][j] = 1;
- }
- mat[i][0] = 1;
- }
- }
- for (int i = 0; i < mat.length - 1; i++) {
- for (int j = 0; j <= i; j++) {
- if (j == 0) {
- Space.p(max_dist * (n - i));
- System.out.print(mat[i][j]);
- } else {
- Space.p((2 * max_dist) - Integer.toString(mat[i][j - 1]).length());
- System.out.print(mat[i][j]);
- }
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement