博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 504. Base 7 (C++)
阅读量:7123 次
发布时间:2019-06-28

本文共 626 字,大约阅读时间需要 2 分钟。

题目:

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

分析:

给定一个7进制数,求十进制表示,注意返回的是字符串。

进制转换没什么好说的,注意这道题测试用例是有负数的,且返回值是字符串,记得转成字符串形式。

程序:

class Solution {public:    string convertToBase7(int num) {        if(num == 0) return "0";        string res;        int n = abs(num);        while(n){            res = to_string(n%7) + res;            n /= 7;        }        if(num < 0)            return "-"+res;        else            return res;    }};

转载于:https://www.cnblogs.com/silentteller/p/10727169.html

你可能感兴趣的文章
第十七章 Java的容器(set)
查看>>
【漫谈Java加密技术(三)】
查看>>
解决不死神兔问题(斐波那契数列)
查看>>
谈谈我对数据库三范式的字面理解
查看>>
自我介绍
查看>>
eclipse配置
查看>>
放松娱乐神注释
查看>>
(转)Dubbo扩展点实现细节
查看>>
错误代码1045 Access denied for user 'root'@'localhost' (using password:YES)
查看>>
Java-JavaSE-多线程
查看>>
Visual Studio开发过程中比较常用的快捷键
查看>>
lnmp监控----nagios与飞信整合实现短信监控
查看>>
tomcat压缩版配置
查看>>
GitHub使用教程
查看>>
vue-cli快速搭建Vue脚手架 (vue-cli 2.x 模板)
查看>>
一些偏僻有用的知识点记录(二)
查看>>
window.open 打开子窗体,关闭全部的子窗体
查看>>
Java第二次上机随笔
查看>>
Standard 1.1.x VM与Standard VM的区别
查看>>
Spring框架中的aop操作之二 通过配置文件实现增强
查看>>