--- title: 第四關程式繳交處 tags: 關卡_st --- > [第四關題目](https://hackmd.io/@futurenest/code_challenge_4) > [程式線上編譯環境](https://replit.com/) :::warning 繳交規範(可以複製這裡的喔) `### 自己名字` `#### 題目一` ```python 程式碼 ``` `#### 題目二` ```c++ 程式碼 ``` ::: --- ### 郭尚蓁 ```python n = int(input("請輸入一個數:")) triangle =[] for i in range(n): row = [1 for _ in range(i+1)] # print("row => ", row) print("i =>", i) if i > 1: prev_row = triangle[-1] for j in range(1, len(row)-1): # print("prev_row[j-1] => {} prev_row[j] => {}".format(prev_row[j-1], prev_row[j] )) row[j] = prev_row[j-1] + prev_row[j] triangle.append(row) print(triangle) ``` ### 林永晉 #### 題目一: ```python= n = int(input("請輸入一個數:")) triangle =[] for i in range(n): row = [1 for _ in range(i + 1)] if i > 1: prev_row = triangle[-1] for j in range(1, len(row) - 1): row[j] = prev_row[j - 1] + prev_row[j] triangle.append(row) print(triangle) ``` #### 題目二: ```python= ans = list(eval(input('請輸入陣列:'))) ans.reverse() print(ans) ``` #### 題目三: ```python= def lenth(l): zero = 0 for i in range(l): if lst[i] == 0: zero += 1 return zero def zero(zero): for i in range(zero): lst.remove(0) lst.append(0) lst = list(eval(input('請輸入陣列:'))) zero(lenth(len(lst))) print(lst) ``` --------------------------------------------- ### 張云綺 #### 題目一 ```python= n = int(input('要輸入幾行?')) pascalList = [ ] #計算帕斯卡三角形的值 for i in range(n) : pascalList.append([]) pascalList[i].append(1) for j in range(1,i): pascalList[i].append(pascalList[i-1][j-1]+pascalList[i-1][j]) if(n!=0): pascalList[i].append(1) #輸出帕斯卡三角形 for i in range(n): print(' '*(n-i),end = ' ',sep='') for j in range(0,i+1): print('{0:6}'.format(pascalList[i][j]),end=' ',sep=' ') print() ``` #### 題目二 ##### 第一種解法 ```python= #print any list in [] num = [ ] print(reversed(num)) print(list(reversed(num))) ``` ##### 第二種解法 ```python= # print any list in [] num = [ ] X = sorted(num) print(X) Y = sorted(num,reverse = True) print(Y) ``` #### 題目三 ```python= ``` ----- ### 楊芷瑜 #### 題目一 ```python= ``` #### 題目二 ```python= a=input("輸入一整數陣列") s="" for i in range(len(a)-1,-1,-1): #(0開頭故要先減1,到那一項的後一向停止,後項減1至下一項) s=s+a[i] #將其開始排列 print("[",s,"]") ``` --------------------------------------------- ### 王政翔 #### 題目一 ```cpp= #include <iostream> using namespace std; int main(){ int i, j, n; cin >> n; int array[21][20] = {}; //設置一個二維陣列 for(i = 0; i < n; i++){ for(j = 0; j <= i; j++){ if(j == 0 || j == i || i == n){ array[j][i] = 1; } else { array[j][i] = array[j-1][i-1] + array[j][i-1]; } } }//帕斯卡三角形出現 cout << "["; //開始列印帕斯卡三角形 for(i = 0; i < n; i++){ cout << "["; for(j = 0; j <= i ; j++){ cout << array[j][i]; if(j == i){ break; } else { cout << ", "; } } cout << "]"; if(i == n-1){ break; } else { cout << ", "; } } cout << "]"; return 0; } ``` #### 題目二 ```cpp= #include <iostream> #include <cstring> #include <algorithm> using namespace std; const std::string WHITESPACE = " \n\r\t\f\v"; std::string ltrim(const std::string &s){ size_t start = s.find_first_not_of(WHITESPACE); return (start == std::string::npos) ? "" : s.substr(start); } std::string rtrim(const std::string &s){ size_t end = s.find_last_not_of(WHITESPACE); return (end == std::string::npos) ? "" : s.substr(0, end + 1); } std::string trim(const std::string &s){ return rtrim(ltrim(s)); } int main(){ string text; string delimiter = ","; string nums[256]; getline(cin, text); //比cin還強,可將空格一起輸進來 int count = 0; string num; size_t pos = 0; while ((pos = text.find(delimiter)) != string::npos){ num = text.substr(0, pos); nums[count++] = trim(num); text.erase(0, pos + delimiter.length()); } nums[count] = trim(text); cout << "["; for(int i = count; i >=0; i--){ cout << nums[i]; if (i != 0){ cout << ", "; } } cout << "]"; return 0; } ``` #### 題目三 ```cpp= #include <iostream> #include <cstring> #include <algorithm> using namespace std; const std::string WHITESPACE = " \n\r\t\f\v"; std::string ltrim(const std::string &s){ size_t start = s.find_first_not_of(WHITESPACE); return (start == std::string::npos) ? "" : s.substr(start); } std::string rtrim(const std::string &s){ size_t end = s.find_last_not_of(WHITESPACE); return (end == std::string::npos) ? "" : s.substr(0, end + 1); } std::string trim(const std::string &s){ return rtrim(ltrim(s)); } int main(){ string text; string delimiter = ","; string nums[256]; getline(cin, text); int count = 0; int zeronum = 0; string num; size_t pos = 0; string zero[256]; while ((pos = text.find(delimiter)) != string::npos){ num = text.substr(0, pos); if(num == "0"){ zero[zeronum++] = trim(num); } else { nums[count++] = trim(num); } text.erase(0, pos + delimiter.length() + 1); } nums[count] = trim(text); cout << "["; for(int i = 0; i <= count; i++){ cout << nums[i]; if(i == count && zeronum == 0){ break; } else { cout << ", "; } } if(zeronum != 0){ for(int x = 0; x < zeronum; x++){ cout << zero[x]; if(x != zeronum-1){ cout << ", "; } } } cout << "]"; return 0; } ```