5204 二进制数
Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others)
Total Submissions: 115 Accepted Submissions: 53
Problem Description
给定一个正整数n,要求输出对应的二进制数中所有数码“1”的位置。注意最低位为第0位。例如13的二进制形式为1101,因此数码1的位置为:0,2,3.
Input
输入文件中的第1行为一个正整数d,表示输入文件中测试数据的个数,1<=d<=10,接下来有d个测试数据。每个测试数据占一行,只有一个整数n,1<=n<=106。
Output
输出包括d行,即对输入文件中的每个测试数据,输出一行。第i行,1<=i<=d,以升序的顺序输出第i个测试数据中的整数的二进制形式中所有数码1的位置,位置之间有1个空格,最后一个位置后面没有空格。
Sample Input
2
13
127
Sample Output
0 2 3
0 1 2 3 4 5 6
#include <stdio.h>
#include <string.h>
int a[100];
int main()
{
int n;
int m;
scanf("%d",&n);
while(n--)
{
int i = 0;
memset(a,0,sizeof(a));
scanf("%d",&m);
while(m)
{
a[i++] = m%2;
m /= 2;
}
i--;
for(int j = 0;j<=i;j++)
{
if(a[j]==1)
{
printf("%d",j);
if(j != i)
printf(" ");
else
printf("\n");
}
}
i = 0;
}
return 0;
}