【HDU】1390 Binary Numbers
2018-04-01 11:52
337 查看
[align=left]Problem Description[/align]Given a positive integer n, find the positions of all 1's in its binary representation. The position of the least significant bit is 0.
Example
The positions of 1's in the binary representation of 13 are 0, 2, 3.
Task
Write a program which for each data set:
reads a positive integer n,
computes the positions of 1's in the binary representation of n,
writes the result.
[align=left]Input[/align]The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.
Each data set consists of exactly one line containing exactly one integer n, 1 <= n <= 10^6.
[align=left]Output[/align]The output should consists of exactly d lines, one line for each data set.
Line i, 1 <= i <= d, should contain increasing sequence of integers separated by single spaces - the positions of 1's in the binary representation of the i-th input number.
[align=left]Sample Input[/align]1
13[align=left]Sample Output[/align]0 2 3
这道题两遍AC,第一次因为格式的问题,最后一个数字后面应该没有空格
代码:#include <stdio.h>
#include <string>
int main() {
int N,n,num;
int s[100];
scanf("%d",&N);
while (N--) {
int i=0;
num=0;
scanf("%d",&n);
while (n) {
s[i]=n%2;
n/=2;
i++;
}
for (int k=0; k<i; k++) {
if (s[k]==1) {
num++;
if(num==1)
printf("%d",k);
else
printf(" %d",k);
}
}
printf("\n");
}
return 0;
}
Example
The positions of 1's in the binary representation of 13 are 0, 2, 3.
Task
Write a program which for each data set:
reads a positive integer n,
computes the positions of 1's in the binary representation of n,
writes the result.
[align=left]Input[/align]The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.
Each data set consists of exactly one line containing exactly one integer n, 1 <= n <= 10^6.
[align=left]Output[/align]The output should consists of exactly d lines, one line for each data set.
Line i, 1 <= i <= d, should contain increasing sequence of integers separated by single spaces - the positions of 1's in the binary representation of the i-th input number.
[align=left]Sample Input[/align]1
13[align=left]Sample Output[/align]0 2 3
这道题两遍AC,第一次因为格式的问题,最后一个数字后面应该没有空格
代码:#include <stdio.h>
#include <string>
int main() {
int N,n,num;
int s[100];
scanf("%d",&N);
while (N--) {
int i=0;
num=0;
scanf("%d",&n);
while (n) {
s[i]=n%2;
n/=2;
i++;
}
for (int k=0; k<i; k++) {
if (s[k]==1) {
num++;
if(num==1)
printf("%d",k);
else
printf(" %d",k);
}
}
printf("\n");
}
return 0;
}
相关文章推荐
- HDU 1390 Binary Numbers
- HDU 1390 Binary Numbers
- HDU1390_Binary Numbers【水题】【位运算】
- HDU1390 ZOJ1383 Binary Numbers【水题+输入输出】
- HDU 1390 Binary Numbers
- hdu 1390 Binary Numbers
- hdu1390-Binary Numbers
- 杭电Hdu 1390 Binary Numbers
- Binary Numbers 1390 HDU
- HDU 1390 Binary Numbers(数学题)
- hdu 1390 Binary Numbers
- HDU 1390 || ZOJ 1383 Binary Numbers
- hdu 1390 Binary Numbers
- [恢]hdu 1390
- hdu 1390
- hdu 1390
- hdoj 1390 Binary numbers (itoa函数运用)
- hdu 1390
- hdu 1390
- 杭电OJ题 1390 Binary Numbers解题报告