# 504. Base 7 七进制数

@TOC

## # 题目描述

Given an integer, return its base 7 string representation.

Example 1:

``````Input: 100
Output: "202"
``````

Example 2:

``````Input: -7
Output: "-10"
``````

Note: The input will be in range of [-1e7, 1e7].

## # 解题方法

### # 内建库

``````public class Solution {
public String convertToBase7(int num) {
return Integer.toString(num,7);
}
}
``````

### # BigInteger类

``````import java.math.BigInteger;
public class Solution {
public String convertToBase7(int num) {
return new BigInteger("" + num, 10).toString(7);
}
}
``````

### # 逐位计算

``````public class Solution {
public String convertToBase7(int num) {
if(num == 0){
return "0";
}
StringBuilder ans = new StringBuilder();
boolean isNeg = num < 0;
num = Math.abs(num);
while(num != 0){
ans.append("" + (num % 7));
num /= 7;
}
if(isNeg){
ans.append("-");
}
ans.reverse();
return ans.toString();
}
}
``````

``````class Solution:
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0: return "0"
res = []
sign =  num >= 0
num = abs(num)
while num != 0:
res.append(num % 7)
num //= 7
return ("" if sign else "-") + "".join(map(str, res[::-1]))
``````

### # 倍数相加

``````public class Solution {
public String convertToBase7(int num) {
if (num == 0) return "0";

int sign = (num > 0 ? 1 : -1);
long res = 0;   // 因为base7会比base10的数字要长， 防止越界，必须用long
int digit = 1;  // 位数，从个位开始

num *= sign;

while (num > 0) {
res += (num % 7) * digit;
digit *= 10;
num /= 7;
}
return String.valueOf(res * sign);
}
}
``````

## # 日期

2017 年 4 月 16 日 2018 年 11 月 17 日 —— 美妙的周末，美丽的天气