--- title: 第三關程式繳交處 tags: 關卡_st --- > [第三關題目](https://hackmd.io/@futurenest/code_challenge_3) > [程式線上編譯環境](https://replit.com/) :::warning 繳交規範(可以複製這裡的喔) `### 自己名字` `#### 題目一` ```python 程式碼 ``` `#### 題目二` ```c++ 程式碼 ``` ::: ### 楊苡若 #### 題目一 ```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= ``` ### 林永晉 #### 題目一: ```python= #氣泡排序 def nnum(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(): n = eval(input("n = ")) lst = [] for i in range(n): lst.append(int(input('請輸入資料:'))) final_ns = nnum(lst) print(final_ns) main() ``` #### 題目一: ```python= #選擇排序 def cal(n): lst = [] for i in range(n): lst.append(eval(input('請輸入資料:'))) count = len(lst) for i in range(count - 1): minindex = i for j in range(i + 1,count): if lst[j] < lst[minindex]: minindex = j lst[minindex],lst[i] = lst[i],lst[minindex] print(lst) cal(eval(input('輸入的資料數目:'))) ``` #### 題目二: ```python= def cal(n): for i in range(2, n): if n % i == 0: # 整除,i 是 n 的因數,所以 n 不是質數 return False return True # 都沒有人能整除,所以 n 是質數 def prime(n): num = [] for i in range(2, n + 1): # 產生 2 到 n 的數字 isprime = cal(i) # 判斷 i 是否為質數 if isprime: # 如果是,印出來 num.append(i) print(num) prime(int(input('請輸入一個數字:'))) ``` #### 題目三: ```python= def form(ans): b = format(ans,'b') if b == b[::-1]: print('True') else: print('False') form(eval(input('enter a number:'))) ``` ### 王政翔 #### 題目一 ```cpp= //氣泡排序法 #include <iostream> using namespace std; int main(){ int n; int i, j; int temp; cout << "n = "; cin >> n; int array[n] = {}; for (i = 0; i < n; i++){ cin >> array[i]; } //兩兩比較,若前者大於後者則交換位置 for(i = 0; i < n-1; i++){ for(j = 0; j < n-i-1; j++){ if(array[j] > array[j+1]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } cout <<"["; for(i = 0; i < n; i++){ cout << array[i]; if(i == n-1){ break; } cout << ", "; } cout << "]" << endl;//打印陣列 return 0; } ``` ```cpp= //選擇排序法 #include <iostream> using namespace std; int main(){ int n; int i, j; int temp; cout << "n = "; cin >> n; int array[n] = {}; for (i = 0; i < n; i++){ cin >> array[i]; } //從未排序的數字中挑出最小的數字,並放入已排序的最尾端 for(i = 0; i < n-1; i++){ for(j = i+1; j < n; j++){ if(array[i] > array[j]){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } cout <<"["; for(i = 0; i < n; i++){ cout << array[i]; if(i == n-1){ break; } cout << ", "; } cout << "]" << endl;//打印陣列 return 0; } ``` #### 題目二 ```cpp= #include <iostream> using namespace std; int main(){ int n; int i, j; int x; int counter; int array[105] = {}; cout << "n = "; cin >>n; for(i = 0; i < n-1; i++){ counter = 0; for(j = 1; j <= i+2; j++){ if((i+2)%j == 0){ counter = counter + 1; }//若是質數則counter值為2 } if(counter == 2){ array[x] = i+2;//將質數存進陣列 x++; } } cout << "["; for(i = 0; i < n; i++){ if(array[i] == 0){ break; } //因為array陣列裡有105個數值 //所以遇到0就不輸出了 cout << array[i]; if(array[i+1] == 0){ break;//最後一項不能有逗點 } cout << ", "; } cout << "]"; return 0; } ``` #### 題目三 我自己有點ㄇㄥ 莫名其妙就做出來了!? ```cpp= #include <iostream> using namespace std; int main(){ int n; int i = 0, j, z; int counter; int array[35] = {}; //n <= 2^32-1 cout << "n = "; cin >> n; if(n == 0 || n == 1){ cout << "True"; return 0; } for(i = 0; n > 0; i++){ array[i] = n % 2; n = n / 2; } int a[i] = {}; int x; x = i; for(j = 0,i = i-1; i>=0; j++,i--){ a[j] = array[i]; } for(i = 0, j = x-1; i <= x/2; i++, j--){ if(a[i] == a[j]){ counter = counter + 1; } else { counter = 0; break; } } if(counter == 0){ cout << "False"; } else { cout << "True"; } return 0; } } ``` ### 張云綺 #### 題目一 ``` python def bubble_sort(numlist): N = len(numlist) #總共有幾個數字要排列 for i in range ( N-1 ): #i控制總共需要多少趟(n-1) for j in range(N-1-i): #控制每趟要比較多少次 if numlist[j] > numlist[j+1]: #判斷j和j+1的大小 temp = numlist[j] numlist[j] = numlist[j+1] numlist[j+1] = temp #進行交換 #以上皆為對bubble_sort的定義 list = [ ] #請先在此添入任何一組數字 print('排序前的list=%s'%list) bubble_sort(list) print('排序後的list = %s'%list) ``` #### 題目二 ``` python= def is_prime(n): for i in range(2,n): if n % i == 0 : #如果n被i整除則不是質數 return False #回報錯誤 return True #回報對的 #以上程式為判斷n是否為質數 n = int(input('輸入任何數字:')) for i in range(2, n+1): pass for i in range(2, n+1): i_is_prime = is_prime(i) for i in range(2, n+1): i_is_prime = is_prime(i) if i_is_prime : print(i) ``` #### 題目三 ``` ``` ### 張若聞 #### 題目一 ```c++= #include <iostream> using namespace std; int main() { int n; int i, j, temp; cout << "n = "; 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; } } } cout << "["; for(i = 0; i < n; i++) { cout << a[i]; if(i != n - 1) { cout << ", "; } } cout << "]" << endl; return 0; } ``` #### 題目二 ```c++= ``` ### 楊芷瑜 #### 題目一 ```python= w=[] n=int(input("輸入位數=")) for i in range(n): w.append(int(input("輸入數字內容="))) def bubble_sort(numlist): n=len(numlist) #列陣長度 for i in range(0,n-1): #從第零項開始至n-1項 for j in range(0,n-i-1): #需要幾趟 if w[j]>w[j+1]: #假如前項大於後項,則要互相交換 temp=w[j] w[j]=w[j+1] w[j+1]=temp print(w) ``` #### 題目二 ```python= def is_prime(n): if n<2: return False if n==2: return True for i in range(2,n): if n % i == 0 : #如果n被i整除則不是質數 return False #回報錯誤 return True #回報正確ˋ h=int(input("輸入數字=")) for i in range(1,h): if is_prime(i): print(i) ```