# 題目 * 一位來自墨西哥蒙特瑞技術研究學院(ITESM Campus Monterrey)的學生想發表一種新的數值加密演算法。 演算法步驟如下: 1. 讀入一個整數N,N為欲加密的數字:N = 265 2. 將N當作十進位的數值:X1 = 265(decimal) 3. 把X1由十進制轉為二進制:X1 = 100001001(binary) 4. 計算二進制的X1有幾個1:b1 = 3 5. 把N當作十六進位數值:X2 = 265(hexadecimal) 6. 把X2由十六進制轉為二進制:X2 = 1001100101(binary) 7. 計算二進制的X2有幾個1:b2 = 5 9. 最後的編碼為N xor (b1*b2):265 xor (3*5) = 262 這位學生並未通過這次的計算機組識考試,所以他請求校方在ACM的試題上出一題計算共有幾個位元1的題目,好讓他能順利發表他的數值加密演算法。 你必須寫一個程式能讀入一個整數,然後輸出該整數的b1, b2值。 # 思路 運用封裝的特性宣告題目所需的函式,然後,就依照進位的轉換原則,進一步去運算和找出題目所求的解。 # Code ```C++=1 #include <iostream> #include <vector> //#include <etc/allocator> //using _gpa=__gnu_cxx::pool_allocator<int>; using _vec=std::vector<int/*,_gpa*/>; class __Problem{ private: int counter(const _vec& vec){ int ans=0; for(auto i:vec){ if(i)ans++; } return ans; } int hexadecimal_Cnt(int M){ _vec hexadevec; int s=1, sum=0; while(M){ sum=sum*s+M%10; s*=16;M/=10; } while(sum){ hexadevec.push_back(sum%2); sum/=2; } return counter(hexadevec); } int decimal_Cnt(int M){ _vec devec; while(M){ devec.push_back(M%2); M/=2; } return counter(devec); } public: void answer(int M){ std::cout<<decimal_Cnt(M)<<" "<<hexadecimal_Cnt(M)<<"\n"; } }encrypt; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int N, M; std::cin>>N; while(N--){ std::cin>>M; encrypt.answer(M); } } ```