This article through a problem, explain the idea of doing two-cycle exercise:
Title: Use a double cycle to print as a shape:
Analysis: The first thing to be clear is that we need to print the following categories: String "*", String "" ( space ). For example, the first line, print 4 spaces "", and then print a "*" .
Let's analyze the specific printing situation for each line:
line 0 : Print 4 spaces First, then print 1 "*"
Line 1 : Print 3 spaces First, then print 3 "*"
Line 2 : Print 2 spaces First, then print 5 "*"
Line 3 : Print 1 spaces First, then print 7 "*"
Line 4 : Print 0 spaces First, then print 9 "*"
from the above analysis, it is not difficult to find," line 0" , " line 1" ... " line 4" The number of these lines is repeated, so we can use the outer loop to control the number of rows (that is, print the first few lines). In the interior of each line," printing n spaces " is a repeating process, so you can use the inner loop to control the number of printed spaces. Similarly, in each line of the interior," print n *" is also a repeating process, so you can use the inner loop to control the number of printing "*" . That is: with the outer loop to control the number of printed lines, with two inner loop to control the number of printing space and print the number of "*" . Also note that you will need to wrap the spaces and "*" in each line after you have printed them out.
Pseudo code is available:
Public Static void Main (string[] args) {
int rows = 5;
for (int i = 0; I < number of rows ; i++) {
for (int j = 0; J < number of spaces; j + +) {
System. out. Print ("");
}
for (int j = 0; J < number of "*"; j + +) {
System. out. Print ("*");
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
}
we are using I to represent the number of rows (the first few lines), with J for the number of printed spaces, K for the number of printed "*" , you can get the following table:
The mathematical relationships between the outer and inner loops are analyzed to:
I+j=4 is j=4-i= (rows-1)-I, that is, the number of spaces equals:( Current number of rows -1)- I
K=2*i+1 , that is, the number of "*" Equals: The current number of rows +1
Now, the isosceles triangle can be printed by replacing the Chinese characters in the pseudo-code with the computed relational formula:
Public Static void Main (string[] args) throws Exception {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; J < rows-i-1; j + +) {
System. out. Print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
System. out. Print ("*");
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
}
Operation Result:
Thinking: Now turn the topic into: Print inverted isosceles triangle,
How to achieve it?
Analysis: Compare "positive isosceles triangle" and "inverted isosceles triangle", as follows
It is not difficult to find,"inverted isosceles triangle" the line 0 , is "positive isosceles triangle" The last line, "inverted isosceles triangle" of the line 1 , is "isosceles triangle" the bottom line of the 21st; ... The last line of "inverted isosceles triangle" is the first line of "positive isosceles triangle". That is: the "positive isosceles triangle" of the number of rows in reverse output, it becomes "inverted isosceles triangle." The number of rows that control the "positive isosceles triangle" is the outer loop for (int i = 0; i < rows; i++), which, inreverse order , is changed to a for (int i = rows-1; i>=0; i--), the "inverted isosceles triangle" printing is achieved, as follows:
Public Static void Main (string[] args) throws Exception {
int rows = 5;
for (int i = 0; i < rows; i++) positive isosceles triangle
for (int i = rows-1; I >= 0; i--) {// reverse output of " positive isosceles triangle " and so on.
for (int j = 0; J < rows-i-1; j + +) {
System. out. Print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
System. out. Print ("*");
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
}
Operation Result:
Thinking: Now turn the topic into: Print diamonds,
How to achieve it?
Analysis:
The diamond can be divided into two parts, the "positive isosceles triangle" and "to isosceles triangle",
Therefore, you only need to print "positive isosceles triangle" before printing "to isosceles triangle". It is important to note that the middle line of the diamond is both the last line of "positive isosceles triangle" and the first line of "inverted isosceles triangle". Therefore, when printing "positive isosceles triangle", you do not need to print the last line, as follows:
Public Static void Main (string[] args) throws Exception {
int rows = 5;
Zheng isosceles triangle
for (int i = 0; i < rows; i++) {
for (int i = 0; i < rows-1; i++) {// Change the second parameter of for to row-1from row , i.e. no need to print the last line of " positive isosceles triangle "
for (int j = 0; J < rows-i-1; j + +) {
System. out. Print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
System. out. Print ("*");
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
pour isosceles triangle
for (int i = rows-1; I >= 0; i--) {// reverse output of " positive isosceles triangle " and so on.
for (int j = 0; J < rows-i-1; j + +) {
System. out. Print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
System. out. Print ("*");
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
}
Operation Result:
Thinking:
Now the title becomes: Print Hollow diamond,
How to achieve it?
Analysis:
The difference between a solid diamond and a hollow diamond is that the hollow diamond is the boundary of a solid diamond, such as:
, that is, when you print the "*" of a "solid diamond", you only need tostart the first "*" and the last one at the end of each line "*" * "Print it out, in the middle of each line" * "Replace with a space." That is, to System. out. Print ("*"); to make a judgment, the pseudo code is as follows:
if ( start with the first "*" or end of the last "*") {
System. out. Print ("*");
} Else {// that is, the middle part prints Spaces
System. out. Print ("");
}
The complete code is as follows:
Public Static void Main (string[] args) throws Exception {
int rows = 5;
Zheng isosceles triangle
for (int i = 0; i < rows; i++) {
for (int j = 0; J < rows-i-1; j + +) {
System. out. Print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
if (j = = 0 | | j = 2 * i) {// start the first "*" or the end of the Last "*"
System. out. Print ("*");
} Else {// that is, the middle part prints Spaces
System. out. Print ("");
}
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
pour isosceles triangle
for (int i = rows-1; I >= 0; i--) {
for (int i = rows-2; I >= 0; i--) {//" inverted isosceles triangle " of line 0 (i.e. rows-1) does not need to hit, directly from the 1 Line (rows-2) Start printing
for (int j = 0; J < rows-i-1; j + +) {
System. out. Print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
if (j = = 0 | | j = 2 * i) {// start the first "*" or the end of the Last "*"
System. out. Print ("*");
} Else {// that is, the middle part prints Spaces
System. out. Print ("");
}
}
System. out. println ();// Line of space and "*" after printing, you need to wrap
}
}
Operation Result:
This article by Blue Bridge Software Institute (http://xueyuan.lanqiao.org) Original, reproduced please indicate the source.
Thinking of solving problems in double circulation--Yequn