Advertisement
rdsedmundo

Pascal.java

Mar 17th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package pascal;
  2.  
  3. /**
  4.  *
  5.  * @author Kyl3
  6.  */
  7. import java.util.Scanner;
  8.  
  9. class Space {
  10.  
  11.     public static void p(int count) {
  12.         for (int i = 0; i < count; i++) {
  13.             System.out.print(" ");
  14.         }
  15.     }
  16. }
  17.  
  18. public class Pascal {
  19.  
  20.     public static void main(String[] args) {
  21.         System.out.println("Digite o número de linhas: ");
  22.         int n = new Scanner(System.in).nextInt() + 1;
  23.  
  24.         System.out.println("Digite o espaçamento entre os valores: ");
  25.         int max_dist = new Scanner(System.in).nextInt();
  26.  
  27.         if (max_dist <= 1) {
  28.             max_dist = 2;
  29.         }
  30.  
  31.         int mat[][] = new int[n][n];
  32.         int maxj[] = new int[n];
  33.  
  34.         for (int i = 0; i < n; i++) {
  35.             if (i == 0) {
  36.                 mat[i][i] = 1;
  37.             } else {
  38.  
  39.                 for (int j = 1; j < n; j++) {
  40.                     mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
  41.                     if (mat[i][j] > maxj[j]) {
  42.                         maxj[j] = mat[i][j];
  43.                     }
  44.                 }
  45.  
  46.                 for (int j = i; j < n; j++) {
  47.                     mat[i][j] = 1;
  48.                 }
  49.  
  50.                 mat[i][0] = 1;
  51.             }
  52.         }
  53.  
  54.         for (int i = 0; i < mat.length - 1; i++) {
  55.             for (int j = 0; j <= i; j++) {
  56.                 if (j == 0) {
  57.                     Space.p(max_dist * (n - i));
  58.                     System.out.print(mat[i][j]);
  59.                 } else {
  60.                     Space.p((2 * max_dist) - Integer.toString(mat[i][j - 1]).length());
  61.                     System.out.print(mat[i][j]);
  62.                 }
  63.             }
  64.  
  65.             System.out.println("");
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement