Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dailyprogrammer;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Easy35 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- System.out.println("Enter number: ");
- int input = s.nextInt();
- int rows = (int) ((-1+ Math.sqrt(1+8*input))/2);
- ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
- int countdown=input+1;
- int spillOverAccounter = countdown;
- do{ //setup arraylist
- triangle = new ArrayList<ArrayList<Integer>>();
- for (int i = 0; i < rows; i++) {
- triangle.add(new ArrayList<Integer>());
- }
- int rowdy = rows;
- countdown = spillOverAccounter-1;
- spillOverAccounter--;
- for (int j = 0; j < rows; j++) {
- for (int k = rowdy; k > 0; k--) {
- triangle.get(j).add(0,countdown);
- countdown--;
- }
- rowdy--;
- }
- }
- while (triangle.get(rows-1).get(0)!=1);
- for (int i = 0; i < rows; i++) {
- System.out.println(triangle.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement