# 大園高中多元選修課程-程式內腦大革命 * **為何我選擇這門課** 上高中前我對寫程式的觀念是 "一定要很聰明才可以學會吧?" "是不是每天都要坐在電腦12hr才可寫出好的程式?" "我這麼笨一定寫不好啦!" 但上高中後...... 藝術課老師讓我看了一部影片《智能社會:進退兩難》故事再說祖克伯先生fb的創辦人是如何創造瘋迷全球的社群媒體,我明白這部劇的爭議性但都撇開不說,單純看看他如何激發我對程式興趣的。 裡面有一個片段是祖克伯回到宿舍用光速的快手寫出一個網站,在當下我第一個反應是:哇賽!原來寫程式可以那麼帥,不但帥還能供大家便利、娛樂跟賺錢 後來的我漸漸喜歡上寫程式自主學習課程中我學習寫遊戲,後來在高二的我選擇這們課。 其中我非常感謝老師超級耐心指導且他打破我對於未來想選資工的迷思 "寫程式不一定要很聰明,只需要你有夠清楚的邏輯能力跟一些耐心" "程式寫得好不在於你坐在電腦前多久在於你能不能用最少的code寫出讓人明白且理解" "你不笨!你要知道會來問跟一直請教老師的人會有多笨??我不相信!你的熱誠及耐心已經顯現出來了" * 課程內容(綱要) 這學期市教C++程式語組用Code::Block寫課程中老師不段提醒我們要"做筆記"我記起來了也幫助自己在最後統整時想起每堂課的內容,前面幾堂課老師著重工具介紹跟觀念建立上,後面他介紹"高中生解題系統"練習(由這時我理解到位什麼多人會在上第一堂後打退堂鼓)但我撐下來。 以上課程大略說明,以下的每個程式都出自於老師上課內容以及我的反思會分成很多主題,我以最簡單扼要的方式呈現來我我學習內容。 # 高中生解題系統 ## 文文的求婚 ```cpp= #include <iostream> using namespace std; int main() { int a; while (cin >> a ) if(a%4==0 & a%100!=0){ //a被100除不等於0 "!"代表not cout << "閏年" << endl; } else if(a%400==0){ cout << "閏年" << endl; } else {cout << "平年" << endl; } return 0; } ``` * 反思 這堂課教會了我除法概念 ## Eav的回家作業 ```cpp= #include <iostream> using namespace std; int main() { int a,b,c,d,e; int typenumber; cin >>typenumber; for(int i=typenumber; i>=1; i--){ //格式要記清楚 cin >>a >>b >>c >> d; if((d-c==c-b) and (c-b==b-a)){ e=(d-c)+d; } else if((d/c==c/b) and (c/b==b/a)){ e=(d/c)*d; } else{ e=0; } cout << a <<" " <<b << " "<<c << " " <<d << " " <<e << endl; } return 0; } ``` 反思: "由小問題解決到大問題"(老師的小名言) 教了很重要的for迴圈 ## 因數分解 ```cpp= #include <iostream> using namespace std; int main() { int r; cin >> r; for (int i=2;i<=r;) { if( r %i==0){ r=r/i; cout << i; } else{ i++; } } } ``` 另解 ```cpp= #include <iostream> using namespace std; int main() { int n; // n表示隨便一個要被除的數字 cin>>n; int m; // m代表每個數字個別是多少次方 for(int i=2;i<=n;i++){ // for迴圈是你已知會迴圈幾次 // while迴圈是你步道有多少次迴圈 m=0; while(n%i==0){ n=n/i; m++; } if(m>=2){ cout<<i<<"^"<<m; // 如果m是2代表一個n的因樹的樹有幾次<然後用^表示次方 } if(m==1){ cout<<i; } if(n!=1 && m>=1){ // n!=1代表n還沒除完 cout<<" * "; } } } ``` * 反思 也是在說明for迴圈的注意事項 "算出來的東西要變成條列是用for來" ## 兩數交換 ```cpp= #include <iostream> using namespace std; int swap(int a,int b){ // swap很重要!!! int t=0; t=a; a=b; b=t; } int main(){ int min =0; int aa[4]={33,34,35,36}; for(int i=0; i<60;i++){ min=i; // 像是這兩個for迴圈的限制是<60 // 總共就會跑60*60次 for(int j=i+1;j<60;j++){ if(aa[min]>aa[j]){ min=j; } } swap(aa[i],aa[min]); } for(int i=0;i<60;i++){ cout <<aa[i]<< " § "; } } ``` # 選擇排序法 是一種新概念 為了更清楚了解我放上之前參考的網站 [選擇排序法](https:/https://medium.com/appworks-school/%E5%88%9D%E5%AD%B8%E8%80%85%E5%AD%B8%E6%BC%94%E7%AE%97%E6%B3%95-%E6%8E%92%E5%BA%8F%E6%B3%95%E5%85%A5%E9%96%80-%E9%81%B8%E6%93%87%E6%8E%92%E5%BA%8F%E8%88%87%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F%E6%B3%95-23d4bc7085ff/)(點一下就可以看到了喔) ```cpp= #include <iostream> using namespace std; int swap(int a,int b){ int t=0; t=a; a=b; b=t; cout<<"a:"<<a<<" "<<"b:"<<b; } int main(){ int a; cin>> a; int b; cin>> b; swap(a,b); return 0; } ``` * 反思 這=次的課說明到了函數的概念,跟以後的排列組合很有關 # **函數交換** ## * 使用函數swap寫交換 ```cpp= #include <iostream> using namespace std; void swap(int &a,int &b){ //void是表示不用回傳直接單用;加上&的a,b就是跟著一個地址 int t=0; // 就算今天a,b=c,d地址也會跟著換 t=a; //將t變成一個暫存器 a=b; //把a,b丟到同一個t中交換 b=t; } ``` ## * swap函數寫兩數排列 ```cpp= #include <iostream> using namespace std; void swap(int &a,int &b){ int t=0; t=a; a=b; b=t; } int main() { int aa; //aa,bb,a,b 是互不相關的值 int bb; cin>> aa>>bb; if(aa<bb){ swap(aa,bb); //是一個呼叫函數swap的方法 //aa,bb丟到swap中執行 } cout<<aa<<" "<<bb<<""; } ``` ## * **不用函數swap寫交換** ```cpp= #include <iostream> using namespace std; int main() { int a,b,t; cin>> a >> b; t=0; t=a; a=b; b=t; cout<<a << " " << b ; return 0; } ``` # 遞迴關係式 * 遞迴關係式-簡單版 ```cpp= #include <iostream> using namespace std; int apple(int a,int b){ //這是函式,拿來當作主其他小食物的鍋子 return a+b; } int main() { int t; //這是最大的函式,用來做最大的鍋子把apple的東西都丟回來這裡面 t=apple(88,55); cout<< t <<endl; } ``` ## * 遞迴關係式-複雜版 ```cpp #include <iostream> using namespace std; int apple(int a) { if(a==1) { return 1; }else{ return a*apple(a-1); } } int main() { int t; t=apple(3); cout<< t <<endl; } ``` > * 遞迴關係式流程圖 (老師開始教流程圖,流程圖非常重要是工程師們的溝通方式在後面有專門講解) ![](https://i.imgur.com/3d4blcx.png) # 河內塔 > * 河內塔流程圖 ![](https://i.imgur.com/tFaLAos.png) ## 費氏數列 ```cpp= #include <iostream> using namespace std; int main() { int n; //INT 要先輸入"n"個數字才放到數列中,否則會變成樹入demi[n]等於一個數字 cin >> n; int demi[n]; demi[0]=0; demi[1]=1; for(int i=2;i<=n;i++){ //cout << "DEMI["<<i<<"] = " << demi[i] << endl;(老師用來測試的) demi[i]=demi[i-1]+demi[i-2]; cout << "DEMI["<<i<<"] = " << demi[i] << endl; } // for(int i=0;i<n;i++){ //(這可以知道每個數字出來的數字) //cout<< demi[i] << endl; // //} return 0; } ``` > * 流程圖 ![](https://i.imgur.com/HQxop9z.png) * 反思 第一開始寫程式前須先得知"方程式為何"在進一步畫"流程圖"最後在打程式。 ## 排列組合 * 演算法筆記 動態規劃 http://web.ntnu.edu.tw/~algo/DynamicProgramming.html ```cpp #include <iostream> using namespace std; int apple(int a,int b) { if(a==b or b==0){ return 1; }else{ return (apple(a-1,b-1)+apple(a-1,b)); } } int main() { int c,x,y; cin>> x; cin>>y; c=apple(x,y); cout<< c <<endl; } ``` 下面這個小練習是上節課的內容 ```cpp #include <iostream> using namespace std; int apple(int a,int b) { if(a==b or b==0){ return 1; }else{ return apple(a-1, b-1) + apple(a-1, b) ; } } int main() { int c; c =apple(5,4); cout<< c <<endl; } ``` 這可以搭配巴斯卡三角形理解 ![](https://i.imgur.com/dJdiOfw.png) n=m時那麼就看最右邊那列全都=1 m=0時可以看左邊那列所有a=n * 總結: 我利用上節學習到的遞迴關係式打出"組合"的式子,過程中我到網站找到排組算式,另外了解到[動態規劃](https://zh.wikipedia.org/zh-tw/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92) > ### 動態規劃 動態規劃(英語:Dynamic programming,簡稱DP)是一種在數學、管理科學、電腦科學、經濟學和生物資訊學中使用的,通過把原問題分解為相對簡單的子問題的方式求解複雜問題的方法。 動態規劃常常適用於有重疊子問題和最佳子結構性質的問題,**動態規劃方法所耗時間往往遠少於樸素解法**。 動態規劃背後的基本思想非常簡單。 ## 流程圖 * 流程圖元件 | 開始或結束工作的圖形 | ![](https://i.imgur.com/CEpc84X.png) | 輸入工作的圖形 | ![](https://i.imgur.com/Shw555b.png) | 處理工作的圖形 | ![](https://i.imgur.com/Nd45QED.png) | 條件判斷的圖形 | ![](https://i.imgur.com/zjTEmfp.png) | 工作流向的圖形 | ![](https://i.imgur.com/9qdTP8y.png) | 連接點 | ![](https://i.imgur.com/zFBYRKx.png) * 範例 ![](https://i.imgur.com/y27UjwZ.png) 以之前的流程圖當示範,今天inport n如果n=1,不是的話return n*f(n-1),是的話return道地一行的式子,最後endl * 小結論 一個流程圖包括 1. 表示相應操作的框; 2. 帶箭頭的流程線; 3. 框內外必要的文字說明。 # 課後反思 程式語言在我學習一學期下來是收穫滿滿,儘管我在上課中常常有不懂得此語跟用法但現在都21世紀用電腦或是手機立刻就可以知道答案,我在下學期的自主學當中要視著解決生活中的問題,是我在擔任糾察幹事時發掘的問題希望我能夠將老師所教的運用在我想解決的地方。 # 參考資料及文獻 [高中生解題網站](https:/https://zerojudge.tw//) [智能社會](https:/https://www.gq.com.tw/entertainment/article/netflix-/) [流程圖介紹](https:/http://www2.lssh.tp.edu.tw/~hlf/class-1/lang-c/flow/flow-chat.htm/) https://medium.com/appworks-school/%E5%88%9D%E5%AD%B8%E8%80%85%E5%AD%B8%E6%BC%94%E7%AE%97%E6%B3%95-%E6%8E%92%E5%BA%8F%E6%B3%95%E5%85%A5%E9%96%80-