C++循环的简单使用【闲来写来练练手~】
代码
1
2 #include<iostream.h>
3 #define N 10
4 void main()
5 {
6 char tempChar=' ';
7 //cout<<"实现正三角形"<<"\n"<<endl;
8 for(int i=0;i<N;i++)
9 {
10 for(int k=N-i;k>0;k--)
11 {
12 cout<<" ";
13 }
14 for(int j=0;j<i;j++)
15 {
16 //第二个
17 if(i>=3&&j==1)
18 {
19 cout<<tempChar<<" ";
20 }
21 //倒数第二个
22 else if(i>=4&&j==i-2)
23 {
24 cout<<tempChar<<" ";
25 }
26 //第四个
27 else if(i>=7&&j==3)
28 {
29 cout<<tempChar<<" ";
30 }
31 //倒数第四个
32 else if(i>=8&&j==i-4)
33 {
34 cout<<tempChar<<" ";
35 }
36 else
37 {
38 cout<<"*"<<" ";
39 }
40 }
41 cout<<endl;
42 }
43 //cout<<"实现倒三角形"<<endl;
44 for(int x=N;x>0;x--)
45 {
46 for(int y=N-x;y>0;y--)
47 {
48 cout<<" ";
49 }
50 for(int z=0;z<x;z++)
51 {
52 if(x>=3&&z==1)
53 {
54 cout<<tempChar<<" ";
55 }
56 else if(x>=4&&z==x-2)
57 {
58 cout<<tempChar<<" ";
59 }
60 else if(x>=7&&z==3)
61 {
62 cout<<tempChar<<" ";
63 }
64 else if(x>=8&&z==x-4)
65 {
66 cout<<tempChar<<" ";
67 }
68 else
69 {
70 cout<<"*"<<" ";
71 }
72 }
73 cout<<endl;
74 }
75 cout<<endl;
76 }
2 #include<iostream.h>
3 #define N 10
4 void main()
5 {
6 char tempChar=' ';
7 //cout<<"实现正三角形"<<"\n"<<endl;
8 for(int i=0;i<N;i++)
9 {
10 for(int k=N-i;k>0;k--)
11 {
12 cout<<" ";
13 }
14 for(int j=0;j<i;j++)
15 {
16 //第二个
17 if(i>=3&&j==1)
18 {
19 cout<<tempChar<<" ";
20 }
21 //倒数第二个
22 else if(i>=4&&j==i-2)
23 {
24 cout<<tempChar<<" ";
25 }
26 //第四个
27 else if(i>=7&&j==3)
28 {
29 cout<<tempChar<<" ";
30 }
31 //倒数第四个
32 else if(i>=8&&j==i-4)
33 {
34 cout<<tempChar<<" ";
35 }
36 else
37 {
38 cout<<"*"<<" ";
39 }
40 }
41 cout<<endl;
42 }
43 //cout<<"实现倒三角形"<<endl;
44 for(int x=N;x>0;x--)
45 {
46 for(int y=N-x;y>0;y--)
47 {
48 cout<<" ";
49 }
50 for(int z=0;z<x;z++)
51 {
52 if(x>=3&&z==1)
53 {
54 cout<<tempChar<<" ";
55 }
56 else if(x>=4&&z==x-2)
57 {
58 cout<<tempChar<<" ";
59 }
60 else if(x>=7&&z==3)
61 {
62 cout<<tempChar<<" ";
63 }
64 else if(x>=8&&z==x-4)
65 {
66 cout<<tempChar<<" ";
67 }
68 else
69 {
70 cout<<"*"<<" ";
71 }
72 }
73 cout<<endl;
74 }
75 cout<<endl;
76 }
运行结果: