leetcode 每日一题
题目
题目链接:https://leetcode-cn.com/problems/base-7/submissions/
思路
- 十进制转七进制:除7取余,逆序
- 如果是负数,在前面加“-”
代码
class Solution {
public:
string convertToBase7(int num) {
if(num==0){
return "0";
}
string ans="";
int tt=max(num,-num);
while(tt){
ans.append(1,tt%7+'0');
tt/=7;
}
reverse(ans.begin(),ans.end());
if(num<0)
ans="-"+ans;
return ans;
}
};