1. Triangle on output
The first line is 1 stars, the second row is 3 stars, the third row is 5 stars, the fourth row is 7 stars, and the fifth row 9 stars.
Analysis:The shape of the triangle by the output of the blank and star composition, by analyzing each row output a few spaces, several stars, you can complete the output triangle work.
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int i=0,j=0;
for (i=1;i<=5;i++) {//Control number of rows
For (j=1;j<= (5-i); j + +) {
cout<< "";//Control output space
}
For (j=1;j<= (2*i-1); j + +) {
cout<< "*"//Control output *
}
cout<<endl;//each line for line wrapping
}
return 0;
}
2. Output Lower Triangle
The first line is 9 stars, the second row is 7 stars, the third row is 5 stars, the fourth row is 3 stars, and the fifth row 1 stars.
Analysis: The figure is the opposite of the upper triangular figure and is similar in thought.
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int i=0,j=0;
for (i=1;i<=5;i++) {//Control number of rows
For (j=1;j<= (i-1); j + +) {
cout<< "";
}
For (j=1;j<= (9-2* (I-1)); J + +) {
cout<< "*";
}
cout<<endl;
}
}
3. Output Diamond
A diamond is actually made up of an upper triangle and a lower triangle. Can be output through two for loops
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int i=0,j=0;
for (i=1;i<=5;i++) {
cout<< "T";
For (j=1;j<= (5-i); j + +) {
cout<< "";
}
For (j=1;j<= (2* (i-1) +1); j + +) {
cout<< "*";
}
cout<<endl;
}
for (i=4;i>=1;i--) {
cout<< "T";
For (j=1;j<= (5-i); j + +) {
cout<< "";
}
For (j=1;j<= (2* (i-1) +1); j + +) {
cout<< "*";
}
cout<<endl;
}
cout<<endl;
}
4. Output Yang Hui's triangle
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1
|
|
2
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1
|
|
3
|
|
3
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
1
|
|
4
|
|
6
|
|
4
|
|
1
|
|
|
|
|
|
|
|
|
|
1
|
|
5
|
|
10
|
|
a
|
|
5
|
|
1
|
|
< /td> |
|
|
|
|
|
1
|
|
6
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
1
|
|
7
|
|
|
|
|
|
&nbs P; |
|
|
7
|
&NB SP; |
1
|
|
|
|
1
|
|
8
|
|
|
|
The
|
|
|
|
|
|
|
|
8
|
|
1
|
&NBSP |
1
|
|
9
|
|
36
|
|
84
|
|
126
|
|
126
|
|
84
|
|
36
|
|
9
|
|
1
|
The most striking feature of the Yang Hui's triangle is that each number equals the sum of two digits above it. This is the principle of program writing
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int i,j;
int a[10][21];
for (i=0;i<10;i++) {
for (j=0;j<21;j++) {
a[i][j]=0;
}
}//completes the initialization of the array
A[0][10]=1;
for (i=1;i<10;i++) {
For (j= (10-i); j<= (10+i); j=j+2) {//10+i= (10-i) +2*i+01-1
A[I][J]=A[I-1][J-1]+A[I-1][J+1];
}
}
for (i=0;i<10;i++) {
cout<< "T";
for (j=0;j<21;j++) {
if (a[i][j]==0) {
cout<< "";
}else{
cout<<a[i][j];
}
}
cout<<endl;
}
cout<<endl;
}