--- tags: 解題報告,zj --- # c013. 00488 - Triangle Wave 題目連結: [Zerojudge](https://zerojudge.tw/ShowProblem?problemid=c013) ## 題目說明 輸入整數 $n$ 表接下來有幾行資料 接收輸入資料並依特定格式輸出 ## 想法 1. 分析範例 2. 巢狀迴圈 1. 振幅 2. 頻率 ## 參考答案 ```cpp= #include <iostream> using namespace std; int main() { int n, A,F; cin>>n; for (int i = 0; i<n; i++){ cin>>A>>F; for (int f = 0; f < F; f++){ for (int h = 1;h<=A; h++){ for (int t = 1; t<=h;t++) cout<<h; cout<<endl; } for (int h = A-1; h>=1; h--){ for (int t = 1; t<=h; t++) cout<<h; cout<<endl; } cout<<endl; } } return 0; } ``` [Github](https://github.com/henryleecode23/solve_record/tree/main/zerojudge/c013) ## 解釋 ### 分析範例 範例輸入: ```= 2 3 2 5 3 ``` ###### 題目好像多換行了 第一行輸入`2`表示有2組資料 第二行開始 每行輸入兩個數字分別代表 `振幅`(A) `頻率`(F) 範例輸出: 先觀察振幅為 3 的波浪 ![](https://i.imgur.com/ytVEyKn.png) 一共有<font color=#00FF31>兩個</font><font color=#FF073C>波浪</font> 波浪的每一階由該階的高度所組成 並且高度由 $1 \sim A$ 再從 $A \sim 1$ 每個波浪之間空一行 ### 巢狀迴圈 根據上面可以知道 1. 有 $n$ 筆資料 2. 每筆有 $T$ 個波浪 3. 每個波浪有 $2A-1$ 階 4. 每階高度若為 $H$ 則需輸出 $H$ 個數字 $H$ #### $n$ 筆資料 ```cpp=4 int n, A,F; cin>>n; for (int i = 0; i<n; i++) ``` #### $T$ 個波浪 ```cpp=8 for (int f = 0; f < F; f++){ ... } // line 20ㄉ ``` #### $2A-1$ 階 要注意的是需要從小到大再回到小 簡單用兩個迴圈分別做從$1 \sim A$與$(A-1) \sim 1$ ```cpp= for (int n = 1;n<=A; n++){ for (int t = 1; t<=n;t++) cout<<n; cout<<endl; } for (int n = A-1; n>=1;n--){ for (int t = 1; t<=n; t++){ cout<<n; } cout<<endl; ``` ###### 記得空一行 #### $H$ 個 $H$ 一樣用`for`迴圈輸出就行了 ```cpp= for (int t = 1; t<=n;t++) cout<<n; ``` --- <!-- {%hackmd @hlc23/dark-theme %} -->