【LeetCode】 504. Base 7

来源:互联网 发布:淘宝网运动棉鞋 编辑:程序博客网 时间:2024/06/04 18:27

Given an integer, return its base 7 string representation.

Example 1:
Input: 100
Output: “202”
Example 2:
Input: -7
Output: “-10”

题目就是转换为7进制的数字。

class Solution {public:    string convertToBase7(int num) {        string s;        int fla=0;        if(num<0){            num=-num;            fla=1;        }        if(num==0)s="0";        while(num){            s=to_string(num%7)+s;            num/=7;        }        if(fla)s='-'+s;        return s;    }};
原创粉丝点击