# 演算法課程題解 - 條件判斷 # TOJ 528 ## 題目 https://toj.tfcis.org/oj/pro/528/ 輸出 $abs(n)$ ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n; cin>>n; if(n<0) cout<<-n<<"\n"; else cout<<n<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 529 ## 題目 https://toj.tfcis.org/oj/pro/529/ 輸出 $abs(n-m)$ ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n,m; cin>>n>>m; if(n-m<0) cout<<-(n-m)<<"\n"; else cout<<n-m<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 530 ## 題目 https://toj.tfcis.org/oj/pro/530/ 輸入 $n, m$ ,排序後輸出 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n,m; cin>>n>>m; if(n>m) cout<<m<<" "<<n<<"\n"; else cout<<n<<" "<<m<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 531 ## 題目 https://toj.tfcis.org/oj/pro/531/ 判斷 $m>n$ 是否成立 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n,m; cin>>n>>m; if(m>n) cout<<"true\n"; else cout<<"false\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # Kattis - Oddities ## 題目 https://hackmd.io/@sa072686/Kattis_oddities https://open.kattis.com/problems/oddities 判斷 $n$ 是奇數或是偶數 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n,x; cin>>n; while(n--){ cin>>x; if(x%2==0) cout<<x<<" is even\n"; else cout<<x<<" is odd\n"; } return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 532 ## 題目 https://toj.tfcis.org/oj/pro/532/ 輸出 $p, q$ 當中 2 的倍數以及 3 的倍數分別有幾個數字 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int p,q,a=0,b=0; cin>>p>>q; if(p%2==0) a++; if(p%3==0) b++; if(q%2==0) a++; if(q%3==0) b++; cout<<a<<" "<<b<<'\n'; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 533 ## 題目 https://toj.tfcis.org/oj/pro/533/ 判斷 $n$ 是否介在 $a, b$ 之間 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int a,b,n; cin>>a>>b>>n; if(a<=n && b>=n) cout<<"yes\n"; else cout<<"no\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 534 ## 題目 https://toj.tfcis.org/oj/pro/534/ 詢問 $a, b$ 是否同為奇數或同為偶數 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; if(a%2==b%2) cout<<"yes\n"; else cout<<"no\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 535 ## 題目 https://toj.tfcis.org/oj/pro/535/ 判斷輸入的整數 $n$ 介在哪個範圍,並輸出相對應內容 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #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; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 94 ## 題目 https://toj.tfcis.org/oj/pro/94/ 輸入月份輸出季節 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main() { int n; cin>>n; if(n>=3 && n<=5){ cout<<"Spring!\n"; } else if(n>=6 && n<=8){ cout<<"Summer!\n"; } else if(n>=9 && n<=11){ cout<<"Autumn!\n"; } else{ cout<<"Winter!\n"; } return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 536 ## 題目 https://toj.tfcis.org/oj/pro/536/ 輸入艦艇以及魚雷的左界右界的座標,判斷艦艇是否可以被魚雷打到 ## 解法 By Koios1143 ### 想法 魚雷可以打到艦艇的情況有 2 種 一種是魚雷的範圍直接包含的艦艇 一種是魚雷的範圍包含了部分的艦艇 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n,m,p,q; cin>>n>>m>>p>>q; if((p<=n && q>=m) || (p<=m && p>=n) || (q<=m && q>=n)) cout<<"yes\n"; else cout<<"no\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # Kattis - Quadrant Selection ## 題目 https://hackmd.io/@sa072686/Kattis_quadrant https://open.kattis.com/problems/quadrant 輸入一個 $x, y$ 座標,輸出位在於第幾象限,保證 $x, y$ 不會有等於 0 的情況 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #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; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 537 ## 題目 https://toj.tfcis.org/oj/pro/537/ 輸入三個數字 $a, b, c$ ,判斷是否任兩個數字的和都大於第三個數字 跟三角形判斷是相同意思 ## 解法 By Koios1143 ### 想法 先將三個數字由小到大排序,我們只需要判斷最小的兩個數字和是否大於第三個數字即可 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; if(a>b) swap(a,b); if(b>c) swap(b,c); if(a>b) swap(a,b); if(a+b>c) cout<<"yes\n"; else cout<<"no\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ ###### tags: `SCIST 演算法 題解`