码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-14 00:05:13      阅读:991      评论:0      收藏:0      [点我收藏+]

标签:public   range   order   int   put   size   round   math   lan   

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





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

标签:public   range   order   int   put   size   round   math   lan   

原文地址:http://www.cnblogs.com/xiejunzhao/p/464fc1a6b2c44d3e0fb58a82290ad9e9.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!