# 新鮮人程式實作學習社群 ###### tags: `ISU111a` `ISU` ## 講義 ### [講義連結](https://drive.google.com/drive/folders/1uBabMneprP5Kowo_ibDz02FR6ywFJVIt?usp=sharing) ## 練習平台 ### [Zerojudge平台](https://zerojudge.tw/) * 若無帳號,請到 Zerojudge 註冊帳號 * 學校單位選(改)為義守大學 * 進入「程式實作學習社群」課程 * 從使用者選單 → 「加入課程」(課程代碼: 18z7+A) ## Lec. 01 程式實作檢測原理與平台講解 ### 實作練習 #### [a737. 10041 - Vito's family -- UVa10041](https://zerojudge.tw/ShowProblem?problemid=a737) - **題目** - [UVa onlinejudge出處](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=982) - [Zerojudge收納](https://zerojudge.tw/ShowProblem?problemid=a737) - **題意** - 有個人有很多親戚需要拜訪。想找一間離所有親戚家最近的房子,使得他拜訪所有親戚時,距離的總和是最小值。 (輸入測資後,求距離的最小值) - **解法** - 差異絕對值相加,最小值出現在排好數列的中位數時,用中位數帶入即可。 - 若資料個數為偶數,不需要取中間兩樹平均值,取中間任何一個數當基準計算即可。 - 可以使用 C++ vector 做動態陣列,提升撰寫效率。 - 可以使用 C++ algorithm 之 sort 排序,提升撰寫效率。 * C/C++ 參考題解 ```cpp= #include <stdio.h> #include <stdlib.h> int cmp(const void *a, const void *b) { return *(int *)a-*(int *)b; } int main() { int n,i,j,r,s[500],m,sum; //1. 讀入Case數目(n) scanf("%d",&n); //2. 依次讀入每一個測試的資料 for(i=0; i<n; i++) { //2.1 讀入親戚數(r) scanf("%d",&r); //2.2 讀入門牌號碼 for(j=0; j<r; j++) scanf("%d",&s[j]); //2.3 排序 qsort(s,r,sizeof(int),cmp); //2.4 找中位數 m = s[r/2]; //2.5 計算距離 sum = 0; for(j=0; j<r; j++) { if(s[j]-m>=0) sum+= (s[j]-m); else sum+= (m-s[j]); } //2.6 輸出 printf("%d\n",sum); } return 0; } ``` #### [a012. 10055 - Hashmat the Brave Warrior -- UVa10055](https://zerojudge.tw/ShowProblem?problemid=a012) * C/C++ 參考題解 ```cpp= ``` #### [c014. 10035 - Primary Arithmetic -- UVa10035](https://zerojudge.tw/ShowProblem?problemid=c014) * C/C++ 參考題解 ```cpp= ```