Given an integer, return its base 7 string representation.(七进制)
Example 1:
Input: 100
Output: “202”
Example 2:
Input: -7
Output: “-10”
class Solution {
public:
string convertToBase7(int num) {
string res;
if(num==0)
res="0";
bool neg=num>=0?false:true;
while(num){
res+=to_string(abs(num%7));
num/=7;
}
res+=neg?"-":"";
reverse(res.begin(), res.end());
return res;
}
};