#include <cstdio> unsigned int countSetBits(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int main(){ int n,i,j,bts=0,sparebits, cnt2; scanf("%d",&n); int count = 0,b1; for(i=1; i<=n;i++){ count = countSetBits(i); // printf("i= %d = %d\n",i,count); bts+=count; if(bts>=n)break; } // printf("%d ",bts); // printf("%d ",i); sparebits=bts-n; cnt2=0; for(j=i;j>0;j--){ if(sparebits>0){ count = countSetBits(j); if(count>sparebits){ cnt2++; } else{ sparebits-=count; } } else cnt2++; } printf("%d\n",cnt2); sparebits=bts-n; for(j=i;j>0;j--){ if(sparebits>0){ count = countSetBits(j); if(count>sparebits){ printf("%d ",j); } else{ sparebits-=count; } } else printf("%d ",j); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <cstdio> unsigned int countSetBits(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int main(){ int n,i,j,bts=0,sparebits, cnt2; scanf("%d",&n); int count = 0,b1; for(i=1; i<=n;i++){ count = countSetBits(i); // printf("i= %d = %d\n",i,count); bts+=count; if(bts>=n)break; } // printf("%d ",bts); // printf("%d ",i); sparebits=bts-n; cnt2=0; for(j=i;j>0;j--){ if(sparebits>0){ count = countSetBits(j); if(count>sparebits){ cnt2++; } else{ sparebits-=count; } } else cnt2++; } printf("%d\n",cnt2); sparebits=bts-n; for(j=i;j>0;j--){ if(sparebits>0){ count = countSetBits(j); if(count>sparebits){ printf("%d ",j); } else{ sparebits-=count; } } else printf("%d ",j); } return 0; } |