# UVA 10931 ZeroJudge a132 ## Parity 本篇主要在紀錄我刷題的過程及記錄,我會統整在題目中遇到的錯誤並註解,記錄思考邏輯 <div style="background-color: #2E3B4E; color: #EAEAEA; padding: 8px; border-radius: 6px;"> <strong>🧠 思路:</strong> 這題是我們期中考的題目,因為考試範圍有遞迴所以只能用遞迴寫,啊我之後也懶得想了直接再用遞迴寫一次。這題主要是把十進制轉二進制,運算規則是把10進制的數字每次取2的餘數再把數字更新成數字/2以此類推到數字==0<br> <strong>⚠️ 注意:</strong> 遞迴要記得設停止條件,ans的function每次都要回傳除2的餘數(這個要加起來)和商(更新數字),dividend的function要輸出2進制數字的表示法,阿更新條件也是除2,然後輸出數字mod(2),1就直接輸出1</div> ```cpp #include<iostream> using namespace std; void dividend(int n); int ans(int n); int main() { int n; cin >> n; while (n>0) { cout << "The parity of" << " ";dividend(n);cout << " " << "is" << " " << ans(n) << " " << "(mod 2)." << endl; cin >> n; } return 0; } void dividend(int n) { if (n == 1)cout<<"1"; else { dividend(n / 2); cout << n % 2; } } int ans(int n) { if (n == 1)return 1; else if (n == 0)return 0 ; else return ans(n / 2) + n % 2; } ```