题解 | #二进制数#

二进制数

https://www.nowcoder.com/practice/103dd589fed14457a673c613d8de3841

#include <iostream>
#include <algorithm>
using namespace std;

string itob(int x);
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    string str;
    while(cin>>n)
    {
        str=itob(n);
        cout<<str<<endl;
    }
}

string itob(int x)
{
    string str;
    if(x==0) str="0";
    while(x!=0)
    {
        str.push_back('0'+x%2);
        x=x>>1;
    }
    reverse(str.begin(),str.end());
    return str;
}

全部评论

相关推荐

nbdy:字太多了,写简历不是写自传,亮点难点技能点列出来就行,要简明扼要
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务