504. Base 7

来源:互联网 发布:单片机 http请求 编辑:程序博客网 时间:2024/05/22 04:24

题目来源【Leetcode】

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进制数,比较简单,直接放代码

class Solution {public:    string convertToBase7(int num) {        string re;        int temp = abs(num);        if(num == 0) return "0";        while(temp != 0){            int t = temp%7;            re += to_string(t);            temp = temp/7;        }        reverse(re.begin(),re.end());        if(num < 0) re = "-"+re;        return re;    }};
原创粉丝点击