--- tags: 解題報告,zj --- # a003. 兩光法師占卜術 題目連結: [Zerojudge](https://zerojudge.tw/ShowProblem?problemid=a003) ## 題目說明 輸入2個數字$M, D$ 並依$(M*2+D)\mod 3$的值輸出`普通`、`吉`、`大吉 ` ## 想法 1. 接收輸入值 2. 計算 3. 判斷並輸出 ## 參考答案 :::spoiler 點我查看 Runtime: 2ms Memory: 332KB 時間複雜度:$O(1)$ 空間複雜度:$O(1)$ ```cpp= #include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int M,D; cin>>M>>D; int S = (M*2+D)%3; if (S == 0) cout<<"普通"; else if (S == 1) cout<<"吉"; else cout<<"大吉"; } ``` [Github](https://github.com/henryleecode23/solve_record/tree/main/zerojudge/a003) ::: ## 解釋 ### 接收輸入與計算 ```cpp=4 int M, D; cin>>M>>D; int S = (M*2+D)%3; ``` ### 判斷與輸出 ```cpp=7 if (S == 0) cout<<"普通"; else if (S == 1) cout<<"吉"; else cout<<"大吉"; ``` ## 其他語言解法 :::spoiler C++ switch ```cpp= #include<iostream> using namespace std; signed main(){ int M, D; cin>>M>>D; switch ((M*2+D)%3){ case 0: cout<<"普通"; break; case 1: cout<<"吉"; break; case 2: cout<<"大吉"; break; } return 0; } ``` ::: :::spoiler Python ```python= inp = input().split() M = int(inp[0]) D = int(inp[1]) if ((M*2+D)%3 == 0): print("普通") elif ((M*2+D)%3 == 1): print("吉") else: print("大吉") ``` ::: --- {%hackmd @hlc23/dark-theme %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up