fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. while (cin >> n) {
  7. // 找到n的最高有效位,然后左移结果
  8. int highestBit = 0;
  9. int temp = n;
  10. while (temp > 0) {
  11. highestBit++;
  12. temp >>= 1;
  13. }
  14.  
  15. // 最后剩下的数字是 2*(n - (1<<(highestBit-1))) + 1
  16. int result = 2 * (n - (1 << (highestBit - 1))) + 1;
  17. cout << result << endl;
  18. }
  19. return 0;
  20. }
Success #stdin #stdout 0s 5320KB
stdin
500
stdout
489