Fork me on GitHub

[leetcode-504-Base 7]

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].

string convertToBase7(int num)
     {
         if (num == 0) return "0";
         string ret = "";
         int temp = num;
         if (num < 0) num = -num;         
         while (num)
         {
             ret.insert(ret.begin(), num % 7 + '0');
//ret = to_string(num % 7) + ret;//to_string用法 num
/= 7; } if (temp < 0)ret.insert(ret.begin(), '-'); return ret; }

 

posted @ 2017-05-10 17:01  hellowOOOrld  阅读(118)  评论(0编辑  收藏  举报