fork(2) download
  1. #include<cstdio>
  2. #include<cstdlib>
  3. using namespace std;
  4. int isPowerOfTwo ( int x)
  5. {
  6. return ((x != 0) && ((x & (~x + 1)) == x));
  7. }
  8. main()
  9. {
  10. int t;
  11. scanf("%d",&t);
  12. if(!(t>=1 && t<=5))
  13. exit(0);
  14. while(t--)
  15. {
  16. int p,n=0,i=0,sum=0,f=0;
  17. scanf("%d",&p);
  18. if(!(p>=1 && p<=100000))
  19. exit(0);
  20. if(p>2048)
  21. {
  22. n=0; f=1;
  23. while(p>=2048)
  24. {
  25. p-=2048;
  26. n++;
  27. }
  28. }
  29. if(!isPowerOfTwo(p) && p!=0)
  30. {
  31. while(sum<=p)
  32. {
  33. sum=1<<i;
  34. i++;
  35. }
  36. i--;
  37. i--;
  38. sum=sum>>1;
  39. p-=sum;
  40. n++;
  41. }
  42. if(!isPowerOfTwo(p) && p!=0)
  43. {
  44. while(p>0)
  45. {
  46. sum=1<<--i;
  47. p-=sum;
  48. if(p<0)
  49. p+=sum;
  50. else
  51. n++;
  52. }
  53. }
  54. else
  55. if(!f)
  56. n++;
  57. printf("%d\n",n);
  58. }
  59. }
Success #stdin #stdout 0s 3344KB
stdin
4
10
256
255
4096
stdout
2
1
8
2