# S3 演算法 week 1 ###### tags: `S3 公開資源` :::info 時間:10/02 9:00 ~ 17:00 地點:成大資工系新館 **65203** 教室 主題:C++ 基礎語法(上) 直播連結:https://youtu.be/3nCRKuLEcSY ::: ## 課程內容 - 課程規則介紹 - C++ 語法基礎 - 基本輸入輸出 - 變數 - 條件判斷 - 講義連結:https://hackmd.io/@sa072686/cp/%2F%40sa072686%2FrJpY0i7PS ## 注意事項 1. 請先預習講義序章及初章前半的內容,如果還沒有安裝編輯程式的軟體,可參考 https://hackmd.io/@SCIST/rkpVU9eGo 安裝 2. 學員請先閱讀課前通知信所附的學員須知及學員指引 3. 實體學員請務必在第一堂課繳回課前通知信所附的家長同意書 4. 上課期間請全程配戴口罩 5. 建議學員可以帶自己的筆電,以減少重新設定的時間成本 6. 請假表單:https://forms.gle/2ESVuTezcHCK959H6 # 必做題題解 [TOC] ## 初章 - 第一節 ### [Kattis hello](https://open.kattis.com/problems/hello) 撰寫者:Koying 輸出 `Hello World!` 即可 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } ``` ::: --- ### [TOJ 520](https://toj.tfcis.org/oj/pro/520/) 撰寫者:fishhh 先讀入兩個數字,再以相反的順序輸出 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { int a,b; cin>>a>>b; cout<<b<<" "<<a<<"\n";//記得要加空白 return 0; } ``` ::: --- ## 初章 - 第二節 ### [TOJ 521](https://toj.tfcis.org/oj/pro/521/) 撰寫者:Eason 兩數相減後直接輸出 :::spoiler 參考程式碼 ```cpp= #include<iostream> using namespace std; int main(){ int m, p; cin >> m >> p; cout << m - p << '\n'; return 0; } ``` ::: --- ### [kattis Jack-O'-Lantern Juxtaposition](https://open.kattis.com/problems/jackolanternjuxtaposition) 撰寫者:fishhh 將三個數輸入後,相乘並輸出即可(題目好長XD) :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; cout<<a*b*c<<"\n"; } ``` ::: --- ### [TOJ 522](https://toj.tfcis.org/oj/pro/522/) 撰寫者:Jun-an 輸入一個非負整數x,將其平方後輸出%10的結果 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main(){ int x; cin >> x; cout << x * x % 10 << '\n'; return 0; } ``` ::: --- ### [TOJ 98](https://toj.tfcis.org/oj/pro/98/) 撰寫者:小麥 從299792458開始,一分鐘x60、一小時再x60..... **記得long long** :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { cout << "1 Light-second(LS) is " << (299792458LL) << " metres." << "\n"; cout << "1 Light-minute(LM) is " << (299792458LL*60) << " metres." << "\n"; cout << "1 Light-hour(LH) is " << (299792458LL*60*60) << " metres." << "\n"; cout << "1 Light-day(LD) is " << (299792458LL*60*60*24) << " metres." << "\n"; cout << "1 Light-week(LW) is " << (299792458LL*60*60*24*7) << " metres." << "\n"; cout << "1 Light-year(LY) is " << (299792458LL*60*60*24*365) << " metres." << "\n"; return 0; } ``` ::: --- ## 初章 - 第三節 ### [TOJ 528](https://toj.tfcis.org/oj/pro/528/) 撰寫者:fishhh 題意:把流動人數讀進來,做絕對值 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main(){ int a; cin>>a; if(a<0){ cout<<-1*a<<"\n"; //或是 -a } else{ cout<<a<<"\n"; } } ``` ::: --- ### [TOJ 531](https://toj.tfcis.org/oj/pro/531/) 撰寫者:Jun-an 輸入兩個正整數 p﹑q, 如果 p < q,輸出 "true", 否則輸出 "false"。 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main(){ int p, q; cin >> p >> q; if(p < q){ cout << "true\n"; } else{ cout << "false\n"; } return 0; } ``` ::: --- ### [TOJ 533](https://toj.tfcis.org/oj/pro/533/) 撰寫者:fishhh 判斷是否a<=n<=b 就ok了 :::spoiler ```cpp= #include "iostream" using namespace std; int main(){ int a,b,n; cin>>a>>b>>n; if(a<=n&&b>=n){ //不能這樣寫ㄛ a<=n<=b cout<<"yes\n"; } else{ cout<<"no\n"; } } ``` ::: --- ### [TOJ 535](https://toj.tfcis.org/oj/pro/535/) 撰寫者:小麥 用if.. else if... else if... else 就解決了 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; if (n == 100) { cout << "S" << "\n"; } else if (n >= 90) { cout << "A" << "\n"; } else if (n >= 80) { cout << "B" << "\n"; } else if (n >= 70) { cout << "C" << "\n"; } else if (n >= 60) { cout << "D" << "\n"; } else { cout << "F" << "\n"; } return 0; } ``` ::: --- ### [TOJ 536](https://toj.tfcis.org/oj/pro/536/) 撰寫者:小麥 考慮三種狀況,只有左邊在瞄準的範圍、只有左邊在瞄準的範圍裡、兩邊都在瞄準的範圍外。 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { int n, m, o, p; cin >> n >> m >> o >> p; if ((n <= o && o <= m) || (n <= p && p <= m) || (o <= n && m <= p)) { cout << "yes" << "\n"; } else { cout << "no" << "\n"; } return 0; } ``` ::: --- ### [Kattis quadrant](https://open.kattis.com/problems/quadrant) 撰寫者:Jun-an 判斷每個象限 x﹑y 的正負關係。 第一象限:(+, +) 第二象限:(-, +) 第三象限:(-, -) 第四象限:(+, -) :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { int x, y; cin >> x >> y; if(x >= 0 && y >= 0){ cout << "1\n"; } else if(x < 0 && y >= 0){ cout << "2\n"; } else if(x < 0 && y < 0){ cout << "3\n"; } else{ cout << "4\n"; } return 0; } ``` ::: --- ### [TOJ 515](https://toj.tfcis.org/oj/pro/515/) 撰寫者:小麥 要先確定是不是 <= 9999 && >= 1000,還有要確定 n % 1111 是否為0。 :::spoiler 參考程式碼 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; if ((1000 <= n && n <= 9999) && ((n % 1111 == 0))) { cout << "GREAT!!" << "\n"; } else { cout << "OAQ" << "\n"; } return 0; } ``` ::: --- ### [Kattis cetvrta](https://open.kattis.com/problems/cetvrta) 撰寫者:fishhh 畫個圖 可以發現,一個矩形裡面只會有兩個x,兩個y 那麼 給了三個點後 三個點的x座標 一定會有兩個相等 找出那個不同的 x 就好了 (y 也是 ![](https://hackmd.io/_uploads/HJAdZhLGo.png) :::spoiler 參考程式碼 ```cpp= ```cpp= #include <iostream> using namespace std; int main() { int x1,x2,x3,y1,y2,y3,ans_x,ans_y; cin>>x1>>y1>>x2>>y2>>x3>>y3; if(x1==x2){ ans_x=x3; } else if(x1==x3){ ans_x=x2; } else{ ans_x=x1; } if(y1==y2){ ans_y=y3; } else if(y1==y3){ ans_y=y2; } else{ ans_y=y1; } cout<<ans_x<<" "<<ans_y<<"\n"; } ``` ::: ---