Binary Numbers 1390 HDU
2015-03-30 22:52
330 查看
Binary Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3276 Accepted Submission(s): 1976
Problem Description
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.
Input
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.
Output
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.
Sample Input
1
13
Sample Output
0 2 3
这个题目要输出给定数字转换为二进制后中1的位置
二进制转换问题–除二取余
要注意存储顺序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3276 Accepted Submission(s): 1976
Problem Description
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.
Input
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.
Output
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.
Sample Input
1
13
Sample Output
0 2 3
这个题目要输出给定数字转换为二进制后中1的位置
二进制转换问题–除二取余
要注意存储顺序
#include<stdio.h> #include<iostream> using namespace std; int nBinaryNumber [30] = {0}; int change(int n) { int mod,len = 0; while(n !=0) { mod = n % 2; n =n /2; nBinaryNumber[len++] = mod; } return len; } int main() { int n,d,k,i; scanf("%d",&d); while(d--) { cin >> n; k = change(n); int flag =0; for(i = 0; i < k; i++) if(nBinaryNumber[i] == 1) { if(flag++) cout << " " << i; else {cout << i;flag++;} } cout << endl; } return 0; }
相关文章推荐
- hdu 1390 Binary Numbers
- HDU 1390 Binary Numbers
- hdu1390-Binary Numbers
- HDU1390_Binary Numbers【水题】【位运算】
- HDU 1390 Binary Numbers
- 杭电Hdu 1390 Binary Numbers
- 【HDU】1390 Binary Numbers
- HDU1390 ZOJ1383 Binary Numbers【水题+输入输出】
- HDU 1390 Binary Numbers
- hdu 1390 Binary Numbers
- hdu 1390 Binary Numbers
- HDU 1390 Binary Numbers(数学题)
- HDU 1390 || ZOJ 1383 Binary Numbers
- hdu 1390
- 杭电ACM1390——Binary Numbers
- HDOJ1390 Binary Numbers
- hdu 1390
- hdu 1390
- hdu 1390
- HDU Binary Numbers 1390(转换+统计)