Advertisement
Guest User

Untitled

a guest
Apr 26th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package com.dailyprogrammer;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Easy35 {
  7.  
  8.     /**
  9.      * @param args
  10.      */
  11.     public static void main(String[] args) {
  12.         Scanner s = new Scanner(System.in);
  13.         System.out.println("Enter number: ");
  14.         int input = s.nextInt();
  15.        
  16.  
  17.         int rows = (int) ((-1+ Math.sqrt(1+8*input))/2);
  18.  
  19.         ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
  20.         int countdown=input+1;
  21.         int spillOverAccounter = countdown;
  22.         do{ //setup arraylist
  23.             triangle = new ArrayList<ArrayList<Integer>>();
  24.             for (int i = 0; i < rows; i++) {
  25.                 triangle.add(new ArrayList<Integer>());
  26.             }
  27.  
  28.  
  29.             int rowdy = rows;
  30.             countdown = spillOverAccounter-1;
  31.             spillOverAccounter--;
  32.             for (int j = 0; j < rows; j++) {
  33.                 for (int k = rowdy; k > 0; k--) {
  34.                     triangle.get(j).add(0,countdown);
  35.                     countdown--;
  36.                 }
  37.  
  38.                 rowdy--;
  39.             }
  40.  
  41.         }
  42.         while (triangle.get(rows-1).get(0)!=1);
  43.  
  44.         for (int i = 0; i < rows; i++) {
  45.             System.out.println(triangle.get(i));
  46.         }
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement