Guest User

Untitled

a guest
Sep 22nd, 2017
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Pr05PascalsTriangle {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int lines = Integer.parseInt(scanner.nextLine());
  9.  
  10.         long[][] triangle = new long[lines][];
  11.  
  12.         for (int line = 0; line < lines; line++) {
  13.             int elements = line + 1;
  14.  
  15.             triangle[line] = new long[elements];
  16.             triangle[line][0] = 1L;
  17.             triangle[line][elements - 1] = 1L;
  18.  
  19.             if (line > 1) {
  20.                 for (int col = 1; col < elements - 1; col++) {
  21.                     triangle[line][col] = triangle[line - 1][col - 1] + triangle[line - 1][col];
  22.                 }
  23.             }
  24.         }
  25.  
  26.         StringBuilder sb = new StringBuilder();
  27.  
  28.         for (int row = 0; row < lines; row++) {
  29.             for (int col = 0; col < triangle[row].length; col++) {
  30.                 if (col > 0) {
  31.                     sb.append(" ");
  32.                 }
  33.  
  34.                 sb.append(triangle[row][col]);
  35.             }
  36.  
  37.             sb.append(System.lineSeparator());
  38.         }
  39.  
  40.         System.out.println(sb.toString());
  41.     }
  42. }
RAW Paste Data