Pyramid of numbers in java

I'm trying to print a pyramid in java. It looks something like this: -

9 8 9 8 7 8 9 8 7 6 7 8 9 8 7 6 5 6 7 8 9 8 7 6 5 4 5 6 7 8 9 8 7 6 5 4 3 4 5 6 7 8 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 

I was looking for ways to solve this on the Internet, and I came across this:

 class Pyramid { public static void main(String[] args) { int x = 7; for (int i = 1; i <= x; i++) { for (int j = 1; j <= x - i; j++) System.out.print(" "); for (int k = i; k >= 1; k--) System.out.print((k >= 10) ?+ k : " " + k); for (int k = 2; k <=i; k++) System.out.print((k >= 10) ?+ k : " " + k); System.out.println(); } } } 

Can someone help me figure this out? Here's what I found out: The outer loop grows to 7, while the inner j-loop grows to xi which is 6 for the first iteration of the outer loop, then 5 ... and so on. Thus, basically the left side of the pyramid is just an inverted triangle of empty spaces.

I'm having trouble figuring out what happens in the other two nested loops, and the weird looking if-else parts inside the print statements

+6
source share
9 answers

Skip this step by step. As you already found out, x is a variable representing the height of the pyramid.

then, as you also correctly understood, the first loop indents the current line of numbers

the second cycle will now write the left half of the numbers, but if I understood, it will start with the largest number and decrement, and then the third cycle will increase the numbers again, creating a slightly different pyramid than the one you are looking for.

now weird, if you call them, if-else, are a ternary conditional operator, and the only purpose they perform in this code is to fix the interval between numbers when the pyramid contains a number> = 10 omitting the leading spaces of the number. :)

+5
source

Since the task looks really difficult in reality, it is easy. It may seem difficult at first. Because you are not thinking about the end result, not the route that leads to the result.

To change this, we can use the old coding rule, divide and conquer . This exam teaches us to find similarities in a complex problem that can reduce the underlying problem to a simple task that we are able to perform. In other words, we split our big problem into a few painters that can be solved very easily, and in the end we combine small results with big ones.

So, to start with your problem.

Q1: How to print pytamind numbers?

As we do not know this, let's focus on something else.

To improve our observation, we can add some bacground details.

  1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 0 _ _ _ _ _ _ _ _ 9 _ _ _ _ _ _ _ _ 1 _ _ _ _ _ _ _ 8 9 8 _ _ _ _ _ _ _ 2 _ _ _ _ _ _ 7 8 9 8 7 _ _ _ _ _ _ 3 _ _ _ _ _ 6 7 8 9 8 7 6 _ _ _ _ _ 4 _ _ _ _ 5 6 7 8 9 8 7 6 5 _ _ _ _ 5 _ _ _ 4 5 6 7 8 9 8 7 6 5 4 _ _ _ 6 _ _ 3 4 5 6 7 8 9 8 7 6 5 4 3 _ _ 7 _ 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 _ 8 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 

Now is the time to watch.

From this observation, we can come up with the following ideas.

Idea: a pyramid is a construction of two triangles.

Conclusion: it is easier to write half of this pyramid. So let's rephrase the acidic problem.

Q2: How to write a serial number that looks like a triangle?

It is really simple, we just need two loops in order for the first loop to be responsible for columns, others for rows.

 for(int column = 1; column <= 9; column++) { for(int row = 1; row <= 9; row++) { if(column ## row) { // Observe what will happen if we use == or <= or > or <> System.out.print(row); } else { System.out.print(" "); } } System.out.println(' '); 

}

When you complete your first task, you will be able to print coils, triagles, strings of numbers on screean.

So, when we know how to print a triangle as follows:

  r c 1 2 3 4 5 6 7 8 9 1 _ _ _ _ _ _ _ _ 9 2 _ _ _ _ _ _ _ 8 9 3 _ _ _ _ _ _ 7 8 9 4 _ _ _ _ _ 6 7 8 9 5 _ _ _ _ 5 6 7 8 9 6 _ _ _ 4 5 6 7 8 9 7 _ _ 3 4 5 6 7 8 9 8 _ 2 3 4 5 6 7 8 9 9 1 2 3 4 5 6 7 8 9 

We need to change your code that is more suitable, usually operations in the computer world begin from scratch, and not from one.

  r c 0 1 2 3 4 5 6 7 8 0 _ _ _ _ _ _ _ _ 9 1 _ _ _ _ _ _ _ 8 9 2 _ _ _ _ _ _ 7 8 9 3 _ _ _ _ _ 6 7 8 9 4 _ _ _ _ 5 6 7 8 9 5 _ _ _ 4 5 6 7 8 9 6 _ _ 3 4 5 6 7 8 9 7 _ 2 3 4 5 6 7 8 9 8 1 2 3 4 5 6 7 8 9 

When you have time with this, stop and think.

Why should we repeat all these operations for each row? If we could put some values ​​that we do not need to think and calculate, they are more focused on writing the entire result on the screen.

The solution to this problem is arrays and the apporach concept, known as dynamic programming . In this approach, we are trying to remember some things that will be used for futre operations.

So, like a worm, just assign a number and an array instead of printing them.

  [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [9] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [8] [9] [ ] [ ] [ ] [ ] [ ] [ ] [7] [8] [9] [ ] [ ] [ ] [ ] [ ] [6] [7] [8] [9] [ ] [ ] [ ] [ ] [5] [6] [7] [8] [9] [ ] [ ] [ ] [4] [5] [6] [7] [8] [9] [ ] [ ] [3] [4] [5] [6] [7] [8] [9] [ ] [2] [3] [4] [5] [6] [7] [8] [9] [1] [2] [3] [4] [5] [6] [7] [8] [9] 

In this case you should come up with a code

  int[] array = new int[9]; for(int column = array.length; column > 0 ; column--) { for(int row = 0; row <= array.length; row++) { if(column == row) { array[row-1] = column; } } System.out.println(Arrays.toString(array)); } 

So, what is clear from this code is that for each step we use only one value. It is presented below

  9 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [9] -Step one we put nine 8 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [8] [ ] -Step two we put eight 7 [ ] [ ] [ ] [ ] [ ] [ ] [7] [ ] [ ] 6 [ ] [ ] [ ] [ ] [ ] [6] [ ] [ ] [ ] 5 [ ] [ ] [ ] [ ] [5] [ ] [ ] [ ] [ ] 4 [ ] [ ] [ ] [4] [ ] [ ] [ ] [ ] [ ] 3 [ ] [ ] [3] [ ] [ ] [ ] [ ] [ ] [ ] 2 [ ] [2] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 1 [1] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 

After nine steps, we fill the entire array with numbers.

We still do not see the result on the screen. To do this, we must print a whole array at every step. First we need to print it from left to right and after that to the end.

And the code that does the magic should look like this:

 public static void pyramide(int levels) { int[] tab = new int[levels]; for(int row = tab.length; row > 0; row--) { tab[row-1] = row; //Print left for(int i=0; i < tab.length; i++) { if(tab[i] != 0) { System.out.print(tab[i]); } else { System.out.print(' '); } } //Print right for(int i= tab.length-2; i >= row - 1; i--) { if(tab[i] != 0) { System.out.print(tab[i]); } } System.out.println(""); } } 
+10
source

This is homework. You will learn more by doing it yourself, instead of blindly copying the work of another.

Start with a similar but simpler problem:

  * *** ***** ******* 

Can you print this triangle? Write your own code and verify that it works correctly.

Now change your code to print numbers instead of asterisks:

  1 123 12345 1234567 

Can you print this triangle? Write your own code and verify that it works correctly.

Now change your code again to solve the final problem. It is often easier to approach a complex problem by first solving similar but simpler problems. You can build code reuse from a solution to a simple task to solving a more complex one.

By all means include ideas and methods from the code you found, but don't just copy it blindly. Write your own code using the good points from your code.

+4
source

Here we go

 public class Pyramid { public static void main(String[] args) { int[] arry = new int[10]; for (int i = 1; i <= 9; i++) arry[i] = i; int index = 0; for (int i = 9; i > 0; i--) { int loop = 1, tempLoop = 0; for (int k = 0; k < 9; k++) { if (k < (9 - index)) System.out.print(" "); else System.out.print(arry[k] + " "); } for (int k = 9; k >= i && (tempLoop++) <= index; k--){ System.out.print(arry[k] + " "); } index++; System.out.println(); } } } 
+1
source

Here in two other nested loops:

 for (int k = i; k >= 1; k--) System.out.print((k >= 10) ?+ k : " " + k); for (int k = 2; k <=i; k++) System.out.print((k >= 10) ?+ k : " " + k); 

if the condition k>=10 true, then the value of k will be displayed with the missing space. If it is false, it displays the value of k with a space.

0
source
 public class PrintInterviewPyramid { public static void main(String[] args) { int n=8; for(int i=1;i<n;i=i+2) { for(int j=1;j<i+1;j++) { System.out.println(" "+j); } } System.out.println(""); } } 
0
source
  for (int i = length - 1; i >= 1; i--) { String front = ""; String back = ""; int space = (length - i) - 1; while (space >= 0) { System.out.print(" "); space--; } for (int j = i; j != 0; j--) { front = j + front; if (j != i) { back = back + j; } } System.out.println(front + back); } 
0
source

Think of two patterns.

First template, print left and right.

Second pattern, print each line and check the space with the start point of the print. A.

 public class NumberPyraimd { public static void main(String[] args){ int part = 2; int stage = 5; // set tree stage. if (part == 2){ int cnt = 0; // thinking two part. for (int i = stage; i > 0; i-- ){ for (int j = 1; j <= stage; j++){ if ( stage - j <= cnt){ System.out.print(j+" "); }else{ System.out.print(" "); } } for (int k = stage; k >0 ; k--){ if ( k != stage){ if ( stage- cnt <= k ){ System.out.print(k+" "); }else{ System.out.print(" "); } } } System.out.println(""); cnt++; } }else if ( part == 1){// think whole lines. int gap = 0; for ( int j = 0; j < stage; j++){ for ( int i = 1;i<=stage*2;i++){ if ( Math.abs(i-stage) <= gap){ System.out.print(stage-gap+" "); }else System.out.print(" "); } System.out.println(""); gap++; } } } } 
0
source

I saw this as a potential question for the interview and wanted to try to implement it using the Java 8 stream methods. The solution below:

 import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; public class App { public static void main( String[] args ) { int min = 1; int max = 9; List<List<String>> pyramid = new ArrayList<>(); IntStream.iterate(max, i -> i - 1).limit(max) .forEach(s -> { List<String> pyramidRow = new ArrayList<>(); IntStream.rangeClosed(min, max) .forEach(j -> { if (j < s) pyramidRow.add(" "); else pyramidRow.add(String.valueOf(j)); }); IntStream.iterate(max - 1, i -> i - 1).limit(max-1) .forEach(j -> { if (j < s) pyramidRow.add(" "); else pyramidRow.add(String.valueOf(j)); }); pyramid.add(pyramidRow); }); pyramid.stream() .forEach(pyra -> { pyra.forEach(System.out::print); System.out.println(); }); } } 
0
source

Source: https://habr.com/ru/post/924552/


All Articles