首页 > 代码库 > 504. 十进制转换为7进制(考虑负数的情况)Base 7

504. 十进制转换为7进制(考虑负数的情况)Base 7

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


  1. public class Solution {
  2. public string ConvertToBase7(int num) {
  3. if (num == 0) {
  4. return "0";
  5. }
  6. int number = Math.Abs(num);
  7. string str = "";
  8. while (number > 0) {
  9. int n = number % 7;
  10. str = n.ToString() + str;
  11. number /= 7;
  12. }
  13. if (num < 0) {
  14. str = "-" + str;
  15. }
  16. return str;
  17. }
  18. }



null


504. 十进制转换为7进制(考虑负数的情况)Base 7