您的位置:首页 > 其它

Hdoj 2051 Bitset

2018-01-13 15:43 204 查看
Problem Description

Give you a number on base ten,you should output it on base two.(0 < n < 1000)

Input

For each case there is a postive number n on base ten, end of file.

Output

For each case output a number on base two.

Sample Input

1

2

3

Sample Output

1

10

11

Author

8600 && xhd

Source

校庆杯Warm Up

题目分析

将一位10进制数转换为2进制数,用位操作实现,每次取出二进制时的最后一位,然后放到数组里面,再用>>去掉二进制的最后一位,注意输出的时候的顺序是反过来的

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 ll;
int len;
int a[70];
int transbit(int n)
{
memset(a,0,sizeof(a));
int num=0;
while(n>0)
{
if(n&1) a[++num]=1;
else    a[++num]=0;
n>>=1;
}
return num;
}
int main()
{
int n;
while(cin>>n)
{
int len = transbit(n);
for(int i=len;i>=1;i--)
{
cout<<a[i];
}
cout<<endl;
}
return 0 ;
}


更多问题请关注个人博客,不定时更新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: