题目描述:
给定一个整数,将其转化为7进制,并以字符串形式输出。
示例 1:
输入: 100
输出: "202"
示例 2:
输入: -7
输出: "-10"
注意: 输入范围是 [-1e7, 1e7] 。
解法:
class Solution {
public:
string convertToBase7(int num) {
string res = "";
bool neg = false;
if(num == 0){
return "0";
}else if(num < 0){
neg = true;
num = -num;
}
while(num != 0){
res = char('0' + num%7) + res;
num /= 7;
}
if(neg){
res = '-' + res;
}
return res;
}
};