C++迴圈的簡單使用【閑來寫來練練手~】

來源:互聯網
上載者:User
代碼

 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 }  

 運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.