Task: | Sum of ones |
Sender: | aalto2024e_006 |
Submission time: | 2024-09-30 16:58:51 +0300 |
Language: | C++ (C++11) |
Status: | COMPILE ERROR |
Compiler report
input/code.cpp: In function 'int countSetBits(int)': input/code.cpp:23:17: error: 'pow' was not declared in this scope 23 | return (x * pow(2, (x - 1))) + (n - pow(2, x) + 1) | ^~~
Code
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int findLargestPower(int n) { int x = 0; while ((1 << x) <= n) x++; return x - 1; } int countSetBits(int n) { if (n <= 1) return n; int x = findLargestPower(n); return (x * pow(2, (x - 1))) + (n - pow(2, x) + 1) + countSetBits(n - pow(2, x)); } int main() { int x; cin >> x; cout << countSetBits(x) << endl; return 0; }