1. 题目
https://tianchi.aliyun.com/oj/118289365933779217/122647324212270017
Given an integer, return its base 7 string representation.
输入范围为[-1e7, 1e7] 。
代码语言:javascript复制示例
样例 1:
输入: num = 100
输出: 202
样例 2:
输入: num = -7
输出: -10
2. 解题
- 除以base得到余数,对所有的余数逆序
class Solution {
public:
/**
* @param num: the given number
* @return: The base 7 string representation
*/
string convertToBase7(int num) {
// Write your code here
bool negative = num < 0;
if(negative)
num = -num;
string ans;
int base = 7;
do
{
ans = (num�se) '0';
num /= base;
}while(num);
reverse(ans.begin(), ans.end());
if(negative)
ans = "-" ans;
return ans;
}
};