fork(4) download
  1. /*input
  2. 11
  3. 16
  4. 1
  5. 10
  6. 8
  7. 3
  8. 5
  9. 2
  10. 10000000
  11. 5000000
  12. 100000
  13. 9999999
  14. */
  15.  
  16. //ideone:: https://i...content-available-to-author-only...e.com/8uz1t4
  17. //spoj http://w...content-available-to-author-only...j.com/problems/EC_CONB
  18. #include <iostream>
  19. #include <algorithm>
  20. #include <cmath>
  21. #include <vector>
  22. using namespace std;
  23.  
  24.  
  25. int binary(int n){
  26. int num=n,Number=0,i;
  27. vector <int> s;
  28. s.clear();
  29. for(i=0;i<=log2(num);++i){
  30. if(n%2==0) s.push_back(0);
  31. else if(n%2==1) s.push_back(1);
  32. n/=2;
  33. }
  34.  
  35. reverse(begin(s),end(s));
  36. i=0;
  37. for(int j:s)
  38. {
  39. Number += pow(2,i)*j;
  40. ++i;
  41. }
  42.  
  43. return Number;
  44. }
  45.  
  46. int main() {
  47.  
  48.  
  49. int t, n;
  50. cin>>t;
  51. while(t--){
  52. cin>>n;
  53. if(n%2==0)
  54. cout<<binary(n)<<endl;
  55. else
  56. cout<<n<<endl;
  57. }
  58. return 0;
  59. }
  60.  
  61.  
Success #stdin #stdout 0s 3420KB
stdin
11
16
1
10
8
3
5
2
10000000 
5000000
100000
9999999
stdout
1
1
5
1
3
5
1
92441
92441
2755
9999999