Loop algorithm

How to do it:

******* -*****- --***-- ---*--- --***-- -*****- ******* 

Below is my code that I wrote to try to execute the above, but it does not work properly:

  public static void stars(/*int jmlBaris*/) { for ( int i = 7; i >= 1; i-=2) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(""); } for (int i = 1; i <= 7; i+=2) { for (int j = 1; j <= i; j++){ System.out.print("*"); } System.out.println(""); } } public static void main(String[] args) { stars(); } } 
+8
java algorithm
source share
8 answers

Here is how I can write it.

 // three loops public static void stars(int size) { for (int y = 0; y < size; y++) { for (int i = 0; i < y && i < size - y - 1; i++) System.out.print(' '); for (int i = Math.min(y, size - y - 1); i < Math.max(y + 1, size - y); i++) System.out.print('*'); System.out.println(); } } 

or

 // two loops public static void stars(int size) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) System.out.print( (x >= y && x < size - y) || (x >= size - y - 1 && x <= y) ? '*' : ' '); System.out.println(); } } 

or

 // one loop public static void stars(int size) { for (int i = 0; i < size * size; i++) { int y = i / size, x = i % size; System.out.print( (x >= y && x < size - y) || (x >= size - y - 1 && x <= y) ? '*' : ' '); if (x == size - 1) System.out.println(); } } 

Note. Whether it uses one, two or three cycles, the time complexity is O (N ^ 2). A simple way to determine this is by the number of stars O (N ^ 2) produced, regardless of how this is done.

+9
source share

I would do something similar with substrings.

 String a = "*******"; //7 stars String blank = " "; //7 spaces int j = 7; for (int i = 0; i < 7; i++) { if (i > j){ System.out.print(blank.substring(0,i)); System.out.println(a.substring(i,j)); } else{ System.out.print(blank.substring(0,j)); System.out.println(a.substring(j,i)); } j--; } System.out.println(a); 

** Previous editing would not work. Changes made.

It works.

+4
source share

Try something like this code that I compiled on IDEOne (it seems to work): http://ideone.com/9xZ1YB

 class Main { public static void main(String[] args) { stars(); } static void stars() { final int MAX_WIDTH = 7; for (int i = 0; i < 7; ++i) { int width; if (i < 3) width = MAX_WIDTH - i * 2; else if (i > 3) width = (i - 3) * 2 + 1; else width = 1; // Before spaces for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j) { System.out.print(" "); } // Stars for (int j = 0; j < width; ++j) { System.out.print("*"); } // After spaces for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j) { System.out.print(" "); } System.out.println(); } } } 
+2
source share

For those new to algorithms, I would recommend that you break the structure into subparts, and then try to solve the pattern.

For this particular template, it can be divided into several triangles. Then each triangle is solved by different for loops, as shown in the figure below.

dividing the pattern into substructures

 public static void printPattern(int num) { // this loop generates first 4 lines for (int i = 0; i < num / 2 + 1; i++) { // draws the red triangle of '-' for (int j = 0; j < i; j++) { System.out.print("-"); } // draws the green triangle of '*' for (int j = i; j < num / 2 + 1; j++) { System.out.print("*"); } // draws the blue triangle of '*' for (int j = i + 1; j < num / 2 + 1; j++) { System.out.print("*"); } // draws the orange triangle of '-' for (int j = 0; j < i; j++) { System.out.print("-"); } System.out.println(); } /* this loop generates last 3 lines */ for (int i = 0; i < num / 2; i++) { // draws the green triangle of '-' for (int j = i + 1; j < num / 2; j++) { System.out.print("-"); } // draws the red triangle of '*' for (int j = 0; j < i + 2; j++) { System.out.print("*"); } // draws the orange triangle of '*' for (int j = 0; j < i + 1; j++) { System.out.print("*"); } // draws the blue triangle of '-' for (int j = i + 1; j < num / 2; j++) { System.out.print("-"); } System.out.println(); } } 

Using a similar method, you can generate any template.

+1
source share

If you understood correctly, your problem is to indent lines 2-7.

Imagine the same problem when the asterisk symbol is replaced by "x" and the space is replaced by "-". Then you need to draw

 xxxxxxx -xxxxx- --xxx-- ---x--- --xxx-- -xxxxx- xxxxxxx 

This means that you must output 0, 1, 2 spaces before the asterisks in the first, second, lowercase lines, respectively. I give you the details to understand them.

0
source share
  public static void stars(/*int jmlBaris*/){ String starstr = "*"; String blank = "_"; int spaceBlank;; for(int i=7; i>=1;i-=2){ spaceBlank = (7-i)*.5; String starrep = StringUtils.repeat(starstr, i); String blankrep = StrinUtils.repeat(blank, spacesBlank); system.out.println(blankrep + starrep + blankrep); } for(int j=3 j<=7; j+=2){ spaceBlank = (7-j)*.5; starrep = StringUtils.repeat(starstr, j); String blankrep = StrinUtils.repeat(blank, spacesBlank); system.out.println(blankrep + starrep + blankrep); } } public static void main(String[] args){ stars(); } 
0
source share

You have few missing to put a space in your code. I donโ€™t care about the right space, who sees this? But left space is very important!

Try the following:

 public static void stars(/*int jmlBaris*/) { for ( int i = 7; i >= 1; i-=2) { for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */ System.out.print(" "); /* Missing Here */ } /* Missing Here */ for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(""); } for (int i = 1; i <= 7; i+=2) { for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */ System.out.print(" "); /* Missing Here */ } /* Missing Here */ for (int j = 1; j <= i; j++){ System.out.print("*"); } System.out.println(""); } } 
0
source share
  int N = 7; for (int y=0; y<N; y++) { for (int x=0; x<N; x++) System.out.print( (yx)*(Nyx-1)<=0 ? '*' : '-'); System.out.println(); } 

or, more symmetrically,

  int n = 3; for (int y=-n; y<=n; y++) { for (int x=-n; x<=n; x++) System.out.print( y*y>=x*x ? '*' : '-'); System.out.println(); } 
0
source share

All Articles