504. Base 7
Given an integer, return its base 7 string representation.
Example 1:
Input: 100 Output: "202"
Example 2:
Input: -7 Output: "-10"
10进制转成7进制
C++(3ms):
1 class Solution { 2 public: 3 string convertToBase7(int num) { 4 int x = abs(num) ; 5 string str ; 6 do 7 { 8 str = to_string(x%7) + str ; 9 } while (x/=7); 10 return num>=0?str:"-"+str; 11 } 12 };
Java(15ms):
1 public class Solution { 2 public String convertToBase7(int num) { 3 return Integer.toString(num, 7) ; 4 } 5 }
把一个6进制的数转成7进制
public String convertToBase7(int num) {
return Integer.toString(Integer.parseInt(num+"", 6), 7) ;
}