題目連結: https://zerojudge.tw/ShowProblem?problemid=a132
# 解題思路
運用遞迴函式吧!!每次就將此數除以 2 向下尋找,再輸出其除以 2 的餘數即可啦!
(要記得順便記總和喔)
# 程式碼
#include <bits/stdc++.h> | |
using namespace std; | |
void func(int &sum,int n){ | |
if(n){ | |
func(sum,n/2); | |
cout<<(n&1); | |
sum+=(n&1); | |
} | |
} | |
int main(){ | |
int n; | |
while(cin>>n&&n){ | |
int sum=0; | |
cout << "The parity of "; | |
func(sum, n); | |
cout << " is " << sum << " (mod 2)." << endl; | |
} | |
} |