# 1115 第三次社課 :::info 盡量**先想過一次**,如果真的想不出來,再參考解答 **不要複製貼上程式碼**,要多練習才會進步喔 ! 好啦但還是希望有人會看 TAT 我寫得很辛苦 ::: **上課內容**:if / else、while 迴圈 ~~迴圈欸 有料~~ ### [zj l958 AC 的快樂:一個人也好快樂](https://zerojudge.tw/ShowProblem?problemid=l958) **題目簡述:** 輸入一個值 x,判斷該值是否為 100 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x; cin >> x; if (x == 100) cout << "今天也好快樂\n"; else cout << "嗚嗚嗚嗚嗚\n"; } ``` ::: --- ### [zj m569 競程圈術語](https://zerojudge.tw/ShowProblem?problemid=m569) **題目簡述:** 輸入兩個整數 x、y,分別判斷此二值大於還是小於 60 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x, y; cin >> x >> y; if (x < 60) { if (y < 60) cout << "燒雞\n"; else cout << "裝弱\n"; } else { if (y < 60) cout << "假解\n"; else cout << "電神\n"; } } ``` ::: --- ### [zj m563 AC 的快樂:三個人也好快樂](https://zerojudge.tw/ShowProblem?problemid=m563) **題目簡述:** 輸入三個數 x、y、z,判斷其中有 2 個以上是 100。 > 小提示: > 用一個計數器 cnt 去算 > 要記得 cnt 的初始值等於 0 :::spoiler **程式碼:** <br> 第一種寫法 使出 for 迴圈之術 ! ! ```cpp= #include <iostream> using namespace std; int main() { int x, a = 0; for (int i = 0; i < 3; i++) { cin >> x; if (x == 100) a++; } if (a >= 2) cout << "今天也好快樂\n"; else cout << "好啊大家都不寫題啊###\n"; } ``` 另一種神奇解法: ```cpp= #include <iostream> using namespace std; int main() { int x1, x2, x3, a; cin >> x1 >> x2 >> x3; a = (x1 == 100) + (x2 == 100) + (x3 == 100); if (a >= 2) cout << "今天也好快樂\n"; else cout << "好啊大家都不寫題啊###\n"; } ``` ::: --- ### [zj d063 0 與 1](https://zerojudge.tw/ShowProblem?problemid=d063) **題目簡述:** 輸入 0 或 1,把 1 變成 0、0 變成 1。 ~~其實也是第一次社課練習題~~ ~~但既然這裡出現了,就試著用 if / else 寫寫看吧~~ > 小提示: > 用 if / else 就沒什麼好難的了 ~~應該吧~~ :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x; cin >> x; if (x) cout << 0 << "\n"; else cout << 1 << "\n"; } ``` ::: --- ### [zj d068 該減肥了](https://zerojudge.tw/ShowProblem?problemid=d068) **題目簡述:** 輸入一個數 w,如果 w > 50,輸出 w - 1,否則輸出 w。 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x; cin >> x; if (x > 50) cout << x - 1 << "\n"; else cout << x << "\n"; } ``` ::: <br> 不用 if / else,毒瘤的寫法。 :::spoiler **奇怪的程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x; cin >> x; cout << ((x > 50)? x - 1 : x) << "\n"; } ``` ::: <br> --- ### [zj d058 BASIC 的 SGN 函數](https://zerojudge.tw/ShowProblem?problemid=d058) **題目簡述:** 輸入一個數 n,若 n > 0,則輸出 1 ; 若 n = 0,則輸出 0 ; 若 n < 0,則輸出 -1。 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; if (!n) cout << 0 << "\n"; else if (n > 0) cout << 1 << "\n"; else cout << -1 << "\n"; } ``` ::: <br> 不用 if / else,毒瘤的寫法。 :::spoiler **奇怪的程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << ((n > 0)? 1 : ((!n)? 0 : -1)) << "\n"; } ``` ::: <br> --- ### [zj c382 加減乘除](https://zerojudge.tw/ShowProblem?problemid=c382) **題目簡述:** 輸入兩個整數,中間有一個運算符號 + ( 加 )、- ( 減 )、* ( 乘 )、/ ( 除 )。 輸出算式的結果。 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int a, b; string s; cin >> a >> s >> b; if (s == "+") cout << a + b << "\n"; else if (s == "-") cout << a - b << "\n"; else if (s == "*") cout << a * b << "\n"; else cout << a / b << "\n"; } ``` ::: --- ### [zj k200 三人行必有我師](https://zerojudge.tw/ShowProblem?problemid=k200) **題目簡述:** 輸入三個數,輸出這三個數最大的數。 > 小提示: > 用一個變數來儲存目前最大的數,跟下一個數比大。 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int n, a = 0; for (int i = 0; i < 3; i++) { cin >> n; if (n > a) a = n; } cout << a << "\n"; } ``` ::: <br> --- ### [zj m568 通靈猜題](https://zerojudge.tw/ShowProblem?problemid=k200) **題目簡述:** 輸入一個數 x,將其各個位數之和取 4 的餘數,根據其結果輸出值。 > 小提示: > 怎麼求各位數的值 ? > 取餘數 > ex : > 485 % 10 = 5 > 48 % 10 = 8 > 4 % 10 = 4 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x, sum = 0; cin >> x; while (x) { sum += x % 10; x /= 10; } sum %= 4; if (!sum) cout << 0 << "\n"; else if (sum == 1) cout << 1 << "\n"; else if (sum == 2) cout << 2 << "\n"; else cout << 3 << "\n"; } ``` ::: <br> --- ### [zj e780 禮物拋接遊戲](https://zerojudge.tw/ShowProblem?problemid=e780) **題目簡述:** 輸入第一行為整數,代表總共有 N 個指令 接下來有 N 行指令,分別為 " L ", " R ", 整數 " Gift 數字 y " 三種任一。 根據指令,輸出可以收到的禮物個數。 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int n, y, x = 0, ans = 0; string s; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "L") x--; else if (s == "R") x++; else { cin >> y; if (x == y) ans++; } } cout << ans << "\n"; return 0; } ``` ::: <br> --- ### [zj f338 后羿射日](https://zerojudge.tw/ShowProblem?problemid=f338) **題目簡述:** 第一行有四個整數表示后羿的 x 座標 x0 與 y 座標 y0 、等級 L0 及射程範圍 R。 第二行有一個整數 N 代表有幾顆太陽。 接下來有 N 行,每行有三個整數,分別代表太陽的 x 座標值 xi、y 座標值 yi。 當與太陽的距離小於射程範圍且太陽等級小於后羿時,可以射下太陽。 輸出一行,表示后羿可以射下幾顆太陽。 > 小提示: > 畢氏定理 :kissing_heart: :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int x0, y0, l0, r, n, x, y, l, ans = 0; cin >> x0 >> y0 >> l0 >> r >> n; for (int i = 0; i < n; i++) { cin >> x >> y >> l; if (l > l0) continue; else { int dx = (x - x0) * (x - x0); int dy = (y - y0) * (y - y0); if (dx + dy <= r * r) ans++; } } cout << ans << "\n"; } ``` ::: <br> --- ### [zj f312 人力分配](https://zerojudge.tw/ShowProblem?problemid=f312) **題目簡述:** 有一個公司有 n 個員工和兩個工廠。工廠一與工廠二分別有 X1 與 X2 個員工,兩個工廠收益 Y1、Y2 分別會是: ![image](https://hackmd.io/_uploads/BJ3rpi2Ea.png =30%x) 考慮所有分配員工的方式,找出收益最大的組合,輸出最大收益 Y1 + Y2。 > 小提示: > 最大收益不一定是正的 :::spoiler **程式碼:** ```cpp= #include <iostream> using namespace std; int main() { int a, b, c, A, B, C, n, sum = -99999999, y; cin >> a >> b >> c >> A >> B >> C >> n; for (int i = 0; i <= n; i++) { int j = n - i; y = a * i * i + b * i + c + A * j * j + B * j + C; if (y > sum) sum = y; } cout << sum << "\n"; } ``` ::: <br> :::warning btw 有寫對的社員悶,偷偷告訴你,這是 APCS 的考題喔喔。 是不是感覺起來沒有很難呢,所以可以去寫寫看我放在社課練習題的 APCS 題目喔 :pray: 其實你會發現,大多數的第一題其實都還算簡單 ! ! ! ::: ---