> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/504.base_7.md).

# 504.Base 7

**504.Base 7**

难度:Easy

> 给定一个整数，将其转化为7进制，并以字符串形式输出。

```
示例 1:

输入: 100
输出: "202"
示例 2:

输入: -7
输出: "-10"
注意: 输入范围是 [-1e7, 1e7] 。
```

按进制算法来，需要考虑负数和0等特殊情况。

```
class Solution {
public:
    string convertToBase7(int num) {
        if(!num) return "0";
        string res;
        bool neg=false;
        if(num<0) 
        {
            neg=true;
            num=-num;
        }
        while(num)
        {
            int tmp=num%7;
            res= (char)('0'+tmp) +res;
            num = (num-tmp)/7;
    }
        return neg?  '-'+ res: res;
    }
};
```

> 执行用时 :0 ms, 在所有 C++ 提交中击败了100.00%的用户\
> 内存消耗 :8 MB, 在所有 C++ 提交中击败了94.40%的用户
