C++打星星---用星號打印圖形


1、輸出N個*;

 1 #include <IOSTREAM.H>
 2 void main()
 3 {
 4     int m;
 5     cout<<"請輸入*的個數:"<<endl;
 6     cin>>m;
 7     while(m)
 8     {
 9         cout<<"*";
10         m--;
11     }
12     cout<<endl;
13 }
View Code

 

2、輸出由N個*組成的矩形:

 1 #include <IOSTREAM>
 2 using namespace std;
 3 void main()
 4 {
 5     int m,n;
 6     cout<<"請輸入*的行數:"<<endl;
 7     cin>>m;
 8     cout<<"請輸入*的列數:"<<endl;
 9     cin>>n;
10     for (int i=0;i<m;i++)
11     {
12         for (int j=0;j<n;j++)
13         {
14             cout<<"*";
15         }
16         cout<<endl;
17     }
18 }

3、輸出由*號組成的左下三角矩陣

 

 1 #include <IOSTREAM>
 2 using namespace std;
 3 void main()
 4 {
 5     int m;
 6     cout<<"請輸入*的行數:"<<endl;
 7     cin>>m;
 8     for (int i=0;i<m;i++) //控制輸出的行數
 9     {
10         for (int j=0;j<=i;j++)//控制前一個循環每一行中的*的個數
11         {
12             cout<<"*";
13         }
14         cout<<endl;
15     }
16 }

4、輸出由*號組成的左上三角矩陣

 1 #include <IOSTREAM>
 2 using namespace std;
 3 void main()
 4 {
 5     int m;
 6     cout<<"請輸入*的行數:"<<endl;
 7     cin>>m;
 8     for (int i=0;i<m;i++) //控制輸出的行數
 9     {
10         for (int j=m-i;j>0;j--)//控制前一個循環每一行中的*的個數
11         {
12             cout<<"*";
13         }
14         cout<<endl;
15     }
16 }

5、輸出由*號組成的右下三角矩陣

 1 #include <IOSTREAM>
 2 using namespace std;
 3 void main()
 4 {
 5     int m;
 6     cout<<"請輸入*的行數:"<<endl;
 7     cin>>m;
 8     for (int i=0;i<m;i++) //控制輸出的行數
 9     {
10             for (int j=m-i;j>0;j--)//控制空格的個數
11             {
12                 cout<<" ";
13             }
14             for (int k=0;k<=i;k++)//控制每一行中的*的個數,與前一個for同時進行
15             {
16                 cout<<"*";
17             }
18             cout<<endl;
19     }
20 }

6、輸出由*號組成的右上三角矩陣

 1 #include <IOSTREAM>
 2 using namespace std;
 3 void main()
 4 {
 5     int m;
 6     cout<<"請輸入*的行數:"<<endl;
 7     cin>>m;
 8     for (int i=0;i<m;i++) //控制輸出的行數
 9     {
10             for (int j=0;j<i;j++)//控制空格的個數
11             {
12                 cout<<" ";
13             }
14             for (int k=m-i;k>0;k--)//控制每一行中的*的個數,與前一個for同時進行
15             {
16                 cout<<"*";
17             }
18             cout<<endl;
19     }
20 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM