首页 > 其他 > 详细

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

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!