fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7. int t, p, arr[12], count;
  8. for(int i=0; i<12; i++) arr[i]=pow(2, i);
  9. cin>>t;
  10. while(t--) {
  11. count=0;
  12. cin>>p;
  13. while(p>0) {
  14. for(int i=11; i>=0; i--) {
  15. if(p>=arr[i]) {
  16. p=p-arr[i]; count++; i++;
  17. }
  18. }
  19. }
  20. cout<<count<<endl;
  21. }
  22. return 0;
  23. }
Success #stdin #stdout 0s 3300KB
stdin
5
10
256
255
4096
4096
stdout
2
1
8
2
2