leetcode 504. Base 7

来源:互联网 发布:linux c 创建文件 编辑:程序博客网 时间:2024/06/01 21:23

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

题意很简单,就是做一个7进制的数据转换

代码如下:

#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;class Solution {public:    string convertToBase7(int num)     {        if (num == 0)            return "0";        int flag = num > 0 ? 1 : -1;        num = abs(num);        string res = "";        while (num > 0)        {            res = to_string(num % 7) + res;            num /= 7;        }        return flag > 0 ? res : "-" + res;    }};
原创粉丝点击