--- title: 訓練場_2程式繳交處 tags: 訓練場_st --- > [訓練場_2 題目](https://hackmd.io/@futurenest/code_training_2) > [程式線上編譯環境](https://replit.com/) :::warning 繳交規範(可以複製這裡的喔) `### 自己名字` `#### 題目一` ```python 程式碼 ``` `#### 題目二` ```c++ 程式碼 ``` ::: ### 簽到囉 !! ### 郭尚蓁 ```python data = [14, 33, 27, 35, 10] print("原始資料:", data) def bubbleSort(data): for i in range(len(data)-1, 0, -1): for j in range(i): if data[j] > data[j+1]: data[j], data[j+1] = data[j+1], data[j] print(data) print('第 %d 次排序結果是:' %(len(data)-i), end='') for j in range(len(data)): print('%3d' %data[j], end='') print('') bubbleSort(data) ``` ``` 原始資料: [14, 33, 27, 35, 10] [14, 33, 27, 35, 10] [14, 27, 33, 35, 10] [14, 27, 33, 35, 10] [14, 27, 33, 10, 35] 第 1 次排序結果是: 14 27 33 10 35 [14, 27, 33, 10, 35] [14, 27, 33, 10, 35] [14, 27, 10, 33, 35] 第 2 次排序結果是: 14 27 10 33 35 [14, 27, 10, 33, 35] [14, 10, 27, 33, 35] 第 3 次排序結果是: 14 10 27 33 35 [10, 14, 27, 33, 35] 第 4 次排序結果是: 10 14 27 33 35 ``` --- ### 張峰嘉 ```cpp #include <iostream> using namespace std; void bubbleSort(int nums[], int n) { for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (nums[j] > nums[j+1]) { int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } } int main() { int nums[] = { 14, 33, 27, 35, 10 }; // n = 5 int n = sizeof(nums)/sizeof(*nums); cout << "排序前 = "; for (int i = 0; i < n; i++) { cout << nums[i]; if (i != n) cout << ", "; } // 換行:'\n' cout << endl; bubbleSort(nums, n); cout << "排序後 = "; for (int i = 0; i < n; i++) { cout << nums[i]; if (i != n) cout << ", "; } cout << endl; return 0; } ``` --- ### 林永晉 #### 氣泡練習 ```python= data = input("請輸入資料:") data = list(eval(n) for n in data.split()) data_count = len(data) for i in range(data_count - 1): for a in range(data_count - 1 - i): if data[a] > data[a + 1]: data[a],data[a + 1] = data[a + 1],data[a] print("第",i,"回合結果:",data) print("排序結束") ``` #### 題目:企鵝 ```python= def panguinsnum(ns): count = len(ns) for i in range(count - 1): for z in range(count - 1 - i): if ns[z] > ns[z + 1]: ns[z],ns[z + 1] = ns[z + 1],ns[z] return ns def main(): panguins = eval(input("請輸入企鵝數量:")) n = [] for i in range(panguins): n.append(int(input('請輸入資料:'))) final_ns = panguinsnum(n) print(final_ns) main() ``` ### 王政翔 #### python ```python data = [14, 33, 27, 35, 10] print("原陣列:", data) for i in range(0, len(data)-1, 1): for j in range(0, len(data)-i-1, 1): if (data[j] > data[j+1]): temp = data[j] data[j] = data[j+1] data[j+1] = temp print("排序後陣列:", data) ``` #### C++ ```cpp #include <iostream> using namespace std; void bubbleSort(int nums[], int n){ for(int i = 0; i < n-1; i++){ for(int j = 0; j < n-i-1; j++){ if(nums[j] > nums[j+1]){ int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } } int main(){ int nums[] = {14, 33, 27, 35, 10}; int i; int temp; int n = sizeof(nums)/sizeof(*nums); for(i = 0; i < n; i++){ cout << nums[i]; if(i == 4){ break; } cout << ","; } cout << endl; bubbleSort(nums, n); for(i = 0; i < n; i++){ cout << nums[i]; if(i == 4){ break; } cout << ","; } return 0; } ``` #### C++ (企鵝) ```cpp= #include <iostream> using namespace std; int main(){ int i; int temp; int n; cin >> n; int nums[n] = {}; for(i = 0; i < n; i++){ cin >> nums[i]; } cout << "原陣列:["; for(i = 0; i < n; i++){ cout << nums[i]; if(i == n-1){ break; } cout << " "; } cout << "]"; for(int i = 0; i < n-1; i++){ for(int j = 0; j < n-i-1; j++){ if(nums[j] > nums[j+1]){ int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } cout << endl; cout << "排序後:["; for(i = 0; i < n; i++){ cout << nums[i]; if(i == n-1){ break; } cout << " "; } cout << "]"; return 0; } ``` ### 張若聞 #### 題目一 ```c++= #include <iostream> using namespace std; int main() { int a[ ] = {14, 33, 27, 35, 10}; int i, j, temp, k; int n = sizeof(a)/sizeof(int); for (i = 0; i < n; i++) { for (k = 0; k < n; k++) cout << a[k] << "\t"; cout << endl; for (j = 0; j < n - i; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } return 0; } ``` #### 題目二 ```c++= #include <iostream> using namespace std; int main() { int n; int i, j, temp; cin >> n; int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } for(i = 0; i < n - 1; i++) { for(j = 0; j < n-i-1; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } for(i = 0; i < n; i++) { cout << a[i] << " "; } return 0; } ``` ### 楊苡若 #### 題目一 ```python= #氣泡排序 length_list = int(input('length:')) my_list = [] for i in range(length_list): Integer = int(input('An integer:')) my_list.append(Integer) for j in range(length_list-1): for s in range(length_list-j-1): if my_list[s] > my_list[s + 1]: my_list[s] , my_list[s + 1] = my_list[s + 1] , my_list[s] print(my_list) ``` ```python= #選擇排序 ``` #### 題目二 ```python= 程式碼 ```