题解 | #进制转换#

进制转换

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

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

int main() {
	int n,b;
	string res, table = "0123456789ABCDEF";
	cin >> n >> b;
    if(n == 0) {
        cout<<"0";
        return 0;
    }
	int flag = 1;
	if (n < 0) {
		flag -= 2;
		n *= -1;
	}
	while (n != 0) {
		res += table[n%b];
		n /= b;
	}
    if(flag == -1) res += "-";
    reverse(res.begin(),res.end());
    cout << res;
	return 0;
}
全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务