题解 | #进制转换#

进制转换

https://www.nowcoder.com/practice/0337e32b1e5543a19fa380e36d9343d7

#include <iostream>
#include <string>
#include <algorithm>
//#define ll long long
using namespace std;

string myDivide(string todo,int x=2);
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string todo,ans;
    while (cin >> todo) 
    {
        ans.clear();
        while(todo!="")
        {
            ans.push_back(((todo[todo.size()-1]-'0')%2)+'0');
            todo=myDivide(todo);
        }
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;
    }
}

string myDivide(string todo,int x)
{
    int reminder=0,pos=0;
    for(int i=0;i<todo.size();++i)
    {
        int temp=(todo[i]-'0')+reminder*10;
        todo[i]=temp/x+'0';
        reminder=temp%x;
    }
    while(todo[0]=='0')
    {
        todo.erase(0,1);
    }
    return todo;

}

全部评论

相关推荐

2024-12-27 10:21
已编辑
海南师范大学 媒介策划
到我怀里来:身高体重住址这些就别写了,留几个关键的就行,工作经历突出重点写详细点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务