- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- for(int i=1;i<=7;i++) //i=0 1 ... 6
- {
- for(int j=1;j<=7-i;j++) //j=6 5 ... 0
- {
- cout<<" ";
- }
- for(int k=1;k<=2*i-1;k++)
- {
- cout<<"*";
- }
- cout<<endl;
- }
-
- for(int i=0;i<=5;i++)
- {
- for(int j=0;j<=i;j++)
- {
- cout<<" ";
- }
- for(int k=0;k<=10-2*i;k++)
- {
- cout<<"*";
- }
- cout<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 宣仲&宣任爸爸提供的程式碼(難度較難,僅參考用)- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int i, j, s, star;
- cout << "How many star do you want?";
- cin >> star;
- for ( i = 0 ; i < star ; i++ )
- {
- s = star / 2 - i;
- if (s < 0)
- s = -s;
- for ( j = 0 ; j < s ; j++ )
- cout << " ";
- for ( j = 0 ; j < star - 2 * s ; j++ )
- cout << "*";
- cout<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |