fork(5) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int T,N,ntest=1;
  7. vector<int> v;
  8. scanf("%d\n",&T);
  9. while(T--)
  10. {
  11. scanf("%d\n",&N);
  12. // To Binary (append an extra zero to the end)
  13. v.clear();
  14. while(N >= 2)
  15. {
  16. v.push_back(N % 2);
  17. N /= 2;
  18. }
  19. v.push_back(N);
  20. v.push_back(0);
  21. // Reverse
  22. reverse(v.begin(), v.end());
  23. // Find next permutation
  24. next_permutation(v.begin(), v.end());
  25. // Transform to decimal
  26. long long ans = 0;
  27. int pos = 0;
  28. for(int i = v.size() - 1; i >= 0; i--)
  29. {
  30. if(v[i])
  31. ans |= (1LL << pos);
  32. pos++;
  33. }
  34. printf("Case %d: %lld\n",ntest++,ans);
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 3232KB
stdin
5
23
14232
391
7
8
stdout
Case 1: 27
Case 2: 14241
Case 3: 395
Case 4: 11
Case 5: 16