题解 | #进制转换#C++暴力遍历解法,注意特殊输入

进制转换

https://www.nowcoder.com/practice/ac61207721a34b74b06597fe6eb67c52

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() 
{
    int M=0;
    int N=0;
    cin>>M>>N;
    string a;
    int count=0;
    if(M==0)
    {
        a+='0';
    }
    else 
    {
        if(M<0)
        {
            count++;
            M*=(-1);
        }
        while(M)
        {
            int m=M%N;
            if(m>=10)
            {
                a+='A'+m-10;
            }
            else
            {
                a+=m+'0';
            }
            M/=N;
        }
    }
    if(count)
    {
        a+='-';
    }
    reverse(a.begin(),a.end());
    cout<<a;
    return 0;
}

全部评论

相关推荐

被普调的六边形战士很高大:项目经历貌似和专业或者求职方向没大关系?
点赞 评论 收藏
分享
04-25 18:13
五邑大学 Java
后来123321:大二两段实习太厉害了,我现在大二连面试都没有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务