1.题目要求:
2.题目代码:
class Solution {
public:string convertToBase7(int num) {string s;int n = abs(num);if(num == 0){s.push_back('0');return s;}//采用迭代法,对基数进行求余while(n){s.push_back(n % 7 + '0');n /= 7;}if(num < 0){s.push_back('-');}//反转字符串reverse(s.begin(),s.end());return s;}
};