给定一个整数,将其转化为7进制,并以字符串形式输出。
示例 1:
输入: 100
输出: "202"
示例 2:
输入: -7
输出: "-10"
class Solution {
public String convertToBase7(int num) {
StringBuffer buf = new StringBuffer();
int yu = 0;
boolean f = false;
if (num < 0) {
num = -1 * num;
f = true;
}
while (num >= 7) {
yu = num % 7;
num = num / 7;
buf.append(yu);
}
buf.append(num);
String str = new String(buf.reverse());
if (f) {
return "-" + str;
}
return str;
}
}