本帖最後由 葉桔良 於 2022-3-19 16:05 編輯
請以for迴圈列出以下圖形
- #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; //star=13
- cout << "How many star do you want?";
- cin >> star;
- for ( i = 0 ; i < star ; i++ ) //i=0 1 2 3 4 5 6 7 8 9 10 11 12
- {
- s = star / 2 - i; //s=6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6
- if (s < 0) //s=6 5 4 3 2 1 0 1 2 3 4 5 6
- s = -s;
- for ( j = 0 ; j < s ; j++ ) //j=6 5 4 3 2 1 0 1 2 3 4 5 6
- cout << " ";
- for ( j = 0 ; j < star - 2 * s ; j++ ) //j=1 3 5 7 9 11 13 11 9 7 5 3 1
- cout << "*";
- cout<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |