fork download
  1. #include<cstdio>
  2. #include <iostream>
  3. int NthIntWithKOneBits(int n, int k)
  4. {
  5. int i, iCk, result=0;
  6.  
  7. if( n == 1 && k == 0 ) return 0;
  8. if( n <= 0 || k <= 0 ) return -1;
  9.  
  10. while( n > 0 && k > 0 )
  11. {
  12. i = k;
  13. iCk = 1;
  14. while( iCk < n )
  15. {
  16. i++;
  17. iCk = iCk * i / (i - k);
  18. }
  19. result |= 1 << i-1;
  20. n -= iCk * (i - k) / i;
  21. k--;
  22. }
  23. return result | (1 << k) - 1;
  24. }
  25. int main(){
  26. return 0;
  27. }
  28.  
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int NthIntWithKOneBits(int, int)’:
prog.cpp:19: warning: suggest parentheses around + or - inside shift
prog.cpp:23: warning: suggest parentheses around arithmetic in operand of |
stdout
Standard output is empty