解题思路:
标准的进制转换题目,直接套模板就好,当然还要注意正负号问题,还有0这个用例,需要在一开始判断出来,代码如下:
class Solution {
public:
string convertToBase7(int num) {
if(num == 0) return "0";
string res;
bool judge = num >= 0;
num = abs(num);
while(num > 0) {
int temp = num % 7;
num /= 7;
res += to_string(temp);
}
if(!judge) {
res += '-';
}
reverse(res.begin(), res.end());
return res;
}
};