看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《vm6jp6rmp4 (Jun)》之銘言: : 今天看書看到了一個題目,畫三角形,想了一個多小時想不出來怎麼畫,圖形如下, : * : *** : ***** : 可不可以問一下,我的高是三,如果我要我的高度可以輸入,有什麼比較好的寫法嗎? : 如果光用指標可以畫嗎?還是一定要用陣列~~~這種畫圖的難度算高嗎? 以下是我的版本XD 可輸入高度, 沒用到指標和陣列 #include <iostream> #include <cstdlib> using namespace std; int main( ) { int i,space,star; //i為第幾行數,space為空白個數,star為星星個數 int n=0; //n為總層數,也可說高 cout<< "請輸入星星金字塔的高:\n"; cin>>n; for(i=1;i<=n;i++) //從第一行開始畫...到第n行 { //開始畫空白 space=n-i; for(int j=1;j<=space;j++) {cout<< " ";} //開始畫星星 star=(2*i)-1; for(int j=1;j<=star;j++) {cout<< "*";} //換行 cout<< "\n"; } system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.231.221.91