# 演算法課程題解 - 變數與運算 # TOJ 521 ## 題目 https://toj.tfcis.org/oj/pro/521/ 給兩個數 $m, p$ 求 $m-p$ ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int m,p; cin>>m>>p; cout<<m-p<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 522 ## 題目 https://toj.tfcis.org/oj/pro/522/ 給一個數字 $x$ ,求 $x^2$ 的個位數字 ## 解法 By Koios1143 ### 想法 平方後模 $10$ 取個位數即可 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int x; cin>>x; x*=x; cout<<x%10<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 523 ## 題目 https://toj.tfcis.org/oj/pro/523/ 給一個數 $x$ ,求他的十位數字 ## 解法 By Koios1143 ### 想法 先除以 $10$ 再模 $10$ 得解 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int x; cin>>x; x/=10; cout<<x%10<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 524 ## 題目 https://toj.tfcis.org/oj/pro/524/ 給兩個數字 $x, y$ ,分別代表 $a+b$ 以及 $a-b$ ,求 $a, b$ ## 解法 By Koios1143 ### 想法 可以藉由 $\frac{x+y}{2}$ 取得 $a$ 再以 $x-a$ 取得 $b$ 即可得解 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int x,y; cin>>x>>y; int a=(x+y)/2; int b=x-a; cout<<"a: "<<a<<", b: "<<b<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 98 ## 題目 https://toj.tfcis.org/oj/pro/98/ 輸出 光秒, 光分, 光時, 光日, 光週, 光年 在公尺的單位下的數值 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main() { long long LS; long long LM; long long LH; long long LD; long long LW; long long LY; long long L=299792458;//m/s LS=L; LM=L*60; LH=L*60*60; LD=L*60*60*24; LW=L*60*60*24*7; LY=L*60*60*24*365; cout<<"1 Light-second(LS) is "<<LS<<" metres."<<endl; cout<<"1 Light-minute(LM) is "<<LM<<" metres."<<endl; cout<<"1 Light-hour(LH) is "<<LH<<" metres."<<endl; cout<<"1 Light-day(LD) is "<<LD<<" metres."<<endl; cout<<"1 Light-week(LW) is "<<LW<<" metres."<<endl; cout<<"1 Light-year(LY) is "<<LY<<" metres."<<endl; return 0; } ``` ### 複雜度分析 總時間複雜度約為 $O(1)$ # TOJ 525 ## 題目 https://toj.tfcis.org/oj/pro/525/ 給兩個正整數 $p, q$ 表示座標上的兩個座標點 求座標上一個整數座標點 $x$ ,使得 $\left|\left| p-x\right| - \left| q-x\right|\right| \le 1$ 若有多組解,輸出座標點最小的那個 ## 解法 By Koios1143 ### 想法 要離兩個點距離差相同,那也就是兩者的中點 可能出現小數點,只需要取整數就可以符合題目要輸出座標點最小的條件 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int p,q; cin>>p>>q; cout<<(int)((p+q)/2)<<"\n"; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ # TOJ 526 ## 題目 https://toj.tfcis.org/oj/pro/526/ 輸入一串數字,輸出數字的相反 例如: 521 輸出 125 不過當前綴是 0 時不需要顯示 ## 解法 By Koios1143 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n,m=0; cin>>n; while(n){ m*=10; m+=(n%10); n/=10; } cout<<m<<'\n'; return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1+log_{10}{n})$ # TOJ 527 ## 題目 https://toj.tfcis.org/oj/pro/527/ 給一個數 $N$ 如果 $N$ 是 $9$ 的倍數就再加上 $9$ 否則輸出一個比他的的數當中,是 $9$ 的倍數的最小值 ## 解法 By Koios1143 ### 想法 在不是 $9$ 的倍數的情況下,可以先藉由 $N \% 9$ 取得 $N$ 與前一個 $9$ 的倍數的距離 用 $9 - (N \% 9)$ 就可以獲得 $N$ 與下一個 $9$ 的倍數的距離了 ### 程式碼 ```cpp= //By Koios1143 #include<iostream> using namespace std; int main(){ int n; cin>>n; if(n%9==0){ cout<<n+9<<"\n"; } else{ cout<<n+(9-n%9)<<"\n"; } return 0; } ``` ### 複雜度分析 總時間複雜度為 $O(1)$ ###### tags: `SCIST 演算法 題解`