504. Base 7

来源:互联网 发布:海康监控软件 编辑:程序博客网 时间:2024/06/15 14:36

题目:

Given an integer, return its base 7 string representation.

Example 1:

Input: 100Output: "202"

Example 2:

Input: -7Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

思路:

本题主要考查的是进制的转换

代码:

class Solution {public:    string convertToBase7(int num) {        string str;        if(num==0)            return "0";        if(num>0){          while(num){            str +=to_string(num%7);            cout<<str<<endl;            num = num / 7;        }            reverse(str.begin(),str.end());        }        else{            num = abs(num);            while(num){            str +=to_string(num%7);            cout<<str<<endl;            num = num / 7;        }            str += "-";            reverse(str.begin(),str.end());                    }          return str;        }};


原创粉丝点击