C++循环的简单使用【闲来写来练练手~】

本文介绍了一段使用C++编程语言绘制特殊正三角形和倒三角形图案的代码。该程序通过复杂的条件判断在特定位置插入空格或字符,以形成独特的视觉效果。通过调整循环和条件判断,可以实现不同大小和样式的三角形。
ExpandedBlockStart.gif 代码
 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  }
 
 

 运行结果:

转载于:https://www.cnblogs.com/wintergrass/archive/2010/09/05/1818552.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值