题解 | #进制转换#

进制转换

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;
}
全部评论

相关推荐

迷茫的大四🐶:你这个拿去投央国企吧,投私企包过不了的
点赞 评论 收藏
分享
09-17 10:53
四川大学 C++
牛客91242815...:会写标书没有任何卵用,鉴定为横向垃圾导师的受害者
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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