Note:
public static void main(String[] args) {
int rows = 5, k = 0, count = 0, count1 = 0;
for(int i = 1; i <= rows; ++i) {
for(int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
++count;
}
while(k != 2 * i - 1) {
if (count <= rows - 1) {
System.out.print((i + k) + " ");
++count;
}
else {
++count1;
System.out.print((i + k - 2 * count1) + " ");
}
++k;
}
count1 = count = k = 0;
System.out.println();
}
}
}
find the answer below:
public class Main {
static int rows = 10;
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<>();
for(int i = 1 ; i <= rows ; i++){
if(i ==1 ) numbers.add(1);
else {
int totalNumber = i * 2 -1;
insertNumbers(totalNumber, numbers);
}
printNumbers(numbers, i);
System.out.println();
numbers.clear();
}
}
private static void insertNumbers(int totalNumber, LinkedList<Integer> numbers) {
int numbersOnEachSide = (totalNumber - 1) / 2;
for (int i = 0; i < numbersOnEachSide ; i++ ) numbers.add((int) Math.pow(2, i));
numbers.add((int) Math.pow(2,numbersOnEachSide));
for(int i = 0 ; i < numbersOnEachSide ; i++) numbers.add((int) Math.pow(2, numbersOnEachSide-1-i));
}
private static void printNumbers(LinkedList<Integer> numbers, int i) {
int spaceNorows = rows - i;
printSpaces(spaceNorows);
for (Integer number : numbers) {
System.out.print(number + "\t");
}
printSpaces(spaceNorows);
}
private static void printSpaces(int spaceNorows) {
for(int j = 0 ; j< spaceNorows ; j++) System.out.print("\t");
}
}
output:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 256 128 64 32 16 8 4 2 1
ps please take a look at https://stackoverflow.com/tour to ask a proper question in later posts.
EDIT : How to run the code in CMD
To run the application above:
1- save all the code above in a file named - for example - Main.java
2- run CMD
in windows or terminal
in linux and compile the Main.java
using javac Main.java
.
3- The compiled file will be put in the same directory as the java
file.
4- run the compiled class using java Main
.
change Main
to whatever name you give to the file
Since you are running the code in the cmd it might not display the 10 row pyramid properly(due to screen resolution, size, etc). So, change the row field in static int rows = 10;
to 5
to see the pyramid properly.
You can further changing the fixed row number to dynamically get it from user.
I decided to give this a try, mainly because I wanted to present some concepts having to do with problem-solving.
First, I made the height of the pyramid a variable. This forced me to consider how different pyramid heights would affect the width of each segment of each line.
Next, I decided to pre-calculate the powers of two. There's no point in recalculating the values for each line. So, I wrote a method to return a precalculated array of integers.
Next, I calculated how much space the largest power of two would take up. I formatted the number using the NumberFormat
class. The NumberFormat
class will format the number according to the Locale
in which you're running the code. I used the format
method of the String
class to give each number enough space.
Next, I divided each line into a blank area, a number area, and a blank area. That way, I could work on each part of a line separately. The blank area on the left side of the pyramid line is the same length as the blank area on the right side of the line, so I only had to create it one time for each line.
I used a StringBuilder
to create each line of the pyramid. It's faster to use a StringBuilder
than to concatenate String
values. I used StringBuilder
segments to create the parts of a pyramid line.
I didn't use static
methods because I wanted to show how you instantiate the class in the main
method. In general, I try to avoid the use of static
methods.
Here's the result of one test run. I assure you, I ran several dozen tests on this code.
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 256 128 64 32 16 8 4 2 1
Here's the code.
import java.text.NumberFormat;
public class NumberPyramid {
public static void main(String[] args) {
int power = 10;
NumberPyramid np = new NumberPyramid();
np.createPyramid(power);
}
private static final NumberFormat NF =
NumberFormat.getNumberInstance();
public void createPyramid(int power) {
int[] values = createPowersArray(power);
String maximum = NF.format(values[power - 1]);
int segmentSize = maximum.length() + 2;
String format = "%" + segmentSize + "s";
for (int index = 0; index < power; index++) {
StringBuilder builder = new StringBuilder();
StringBuilder blankArea = createBlankArea(
index, power, segmentSize);
builder.append(blankArea);
builder.append(createNumberArea(index, values, format));
builder.append(blankArea);
System.out.println(builder.toString());
}
}
private int[] createPowersArray(int power) {
int[] values = new int[power];
values[0] = 1;
for (int i = 1; i < power; i++) {
values[i] = values[i - 1] * 2;
}
return values;
}
private StringBuilder createNumberArea(int index,
int[] values, String format) {
StringBuilder builder = new StringBuilder();
for (int j = 0; j <= index; j++) {
builder.append(String.format(format,
NF.format(values[j])));
}
for (int j = index - 1; j >= 0; j--) {
builder.append(String.format(format,
NF.format(values[j])));
}
return builder;
}
private StringBuilder createBlankArea(int index,
int power, int segmentSize) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < power - index - 1; i++) {
builder.append(createBlankSegment(segmentSize));
}
return builder;
}
private StringBuilder createBlankSegment(int length) {
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
builder.append(" ");
}
return builder;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.