您的位置:首页 > 其它

[leetcode]504. Base 7

2017-07-15 21:10 381 查看
题目链接:https://leetcode.com/problems/base-7/#/description

Given an integer, return its base 7 string representation.

Example 1:

Input: 100
Output: "202"


Example 2:

Input: -7
Output: "-10"

class Solution {
public:
string convertToBase7(int num) {
if(num==0)
return "0";

int number=abs(num);
string res="";
while(number)
{
int n=number%7;
res=to_string(n)+res;
number/=7;
}
if(num<0)
res="-"+res;
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: