把十进制数转成七进制,要求输出字符串
class Solution {
public:
string convertToBase7(int num) {
if(num == 0)
return "0";
string cnum = "";
int flag = 0;
if(num < 0){
flag = 1;
num = -num;
}
while(num){
char ret = num % 7 + '0';
cnum = ret + cnum;
num /= 7;
}
if(flag){
cnum = '-' + cnum;
}
return cnum;
}
};