#include #include int sumTataku( int n ) { int ans = 0; int on_bit_num = 0, dist = 0; int temp = n, dist_calc = 0; do{ if( temp % 2 ){ ++on_bit_num; dist += dist_calc; } ++dist_calc; } while( ( temp = temp >> 1 ) > 0 ); std::cout << "on_bit_num = " << on_bit_num << "\n"; std::cout << "dist = " << dist << "\n"; ans += dist; temp = on_bit_num; if( temp % 2 && temp > 1){ ++ans; } on_bit_num = 0; dist = 0; dist_calc = 0; do{ if( temp % 2 ){ ++on_bit_num; dist += dist_calc; } ++dist_calc; } while( ( temp = temp >> 1 ) > 0 ); std::cout << "on_bit_num2 = " << on_bit_num << "\n"; std::cout << "dist2 = " << dist << "\n"; ans += dist; return ans; } int main() { while( true ){ try{ std::cout << "number : "; std::string strn; std::cin >> strn; int n = std::stoi( strn ); std::cout << "\n ans = " << sumTataku( n ) << std::endl; } catch( ... ){ break; } } return 0; }