logo头像

往者不可谏,来者犹可追。

牛客-北邮-二进制

题目描述

大家都知道,数据在计算机里中存储是以二进制的形式存储的。 有一天,小明学了C语言之后,他想知道一个类型为unsigned int 类型的数字,存储在计算机中的二进制串是什么样子的。 你能帮帮小明吗?并且,小明不想要二进制串中前面的没有意义的0串,即要去掉前导0。


输入描述:

每行有一个数字n(0<=n<=10^8),表示要求的二进制串。


输出描述:

输出共T行。每行输出求得的二进制串。


示例1

输入

23

输出

10111


分析

数学题,除留余数。注意是倒序输出,所以用了栈,也可以不用。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;

int main()
{
stack<char> s;
int n,t;
cin>>n;
while(n){
t = n %2;
if(t) s.push('1');
else s.push('0');
n/=2;
}
while(!s.empty()){
cout<<(s.top());
s.pop();
}
return 0;
}
上一篇