012 Integer to Roman 整数转换成罗马数字
给定一个整数,将其转为罗马数字。输入保证在 1 到 3999 之间。
详见:https://leetcode.com/problems/integer-to-roman/description/
class Solution {
public:
string intToRoman(int num) {
string res = "";
char roman[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int value[] = { 1000, 500, 100, 50, 10, 5, 1 };
for (int n = 0; n < 7; n += 2)
{
int x = num / value[n];
if(x<4)
{
for(int i=1;i<=x;++i)
{
res+=roman[n];
}
}
else if(x==4)
{
res=res+roman[n]+roman[n-1];
}
else if(x>4&&x<9)
{
res+=roman[n-1];
for(int i=6;i<=x;++i)
{
res+=roman[n];
}
}
else if (x == 9)
{
res=res+roman[n]+roman[n-2];
}
num%=value[n];
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/4123374.html
012 Integer to Roman 整数转换成罗马数字的更多相关文章
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...
- [Leetcode] Interger to roman 整数转成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- lintcode :Integer to Roman 整数转罗马数字
题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- 将整数转换成二进制的java小程序
首先我们知道,将整数转换成二进制是将整数除二取余将最后除得的数和得到的余数从下向上写,组成得到的二进制数. java程序实现如下: public class ChangeToErjinzhi { pu ...
- Javascript里,想把一个整数转换成字符串,字符串长度为2
Javascript里,想把一个整数转换成字符串,字符串长度为2. 想把一个整数转换成字符串,字符串长度为2,怎么弄?比如 1 => "01"11 => " ...
- int类型的整数转换成汉字
int类型的整数转换成汉字 一.源代码:IntegerNumberToChinese.java package cn.com.zfc.example; import java.util.Scanner ...
随机推荐
- Operating System-Thread(2) Multi-Process-Parallel vs Multi-Thread-Parallel
本文主要介绍线程的模型 一.Multi-Process-Parallel vs Multi-Thread-Parallel 多进程的并行:CPU在多个进程之间进行切换,系统给人一种多个进程在并行执行的 ...
- cmdb1--介绍
背景:现在运维管理服务器多数使用Excel表来维护,而且是多人来维护,造成信息不统一,所以要将信息入库,并方便后续的批量操作 1.cmdb主要分3块: a.采集信息程序 b.API提供接口 c.后台管 ...
- JAVA 1.5 并发之 BlockingQueue
1.BlockingQueue 顾名思义就是阻塞队列 最经典的使用场合就是 生产者 - 消费者 模型啦,其优点是队列控制已经处理好,用户只需要存(满了会阻塞),取(空了会阻塞) 可以更多的关心核心逻辑 ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...
- 第三课 go语言基础语法
http://www.runoob.com/go/go-basic-syntax.html 1 行分隔符 在 Go 程序中,一行代表一个语句结束.每个语句不需要像 C 家族中的其它语言一样以分号 ; ...
- char与wchar_t数据类型
转自:http://blog.itpub.net/27634692/viewspace-752200/ 有的人爱用strcpy等标准ANSI函数,有的人爱用_tXXXX函数,有必要把来龙去脉搞清楚. ...
- Mac 远程连接Linux服务器及上传、下载命令
1.使用ssh命令连接远程服务器主机 1.不设置端口,默认就是22 ssh root@192.168.18.129 1.1.设置端口例: ssh -p 22 root@192.168.18.1292. ...
- Vue之vue.js声明式渲染
Html: <div id="app"> {{ message }} </div> Vue: var app = new Vue({ el: '#app', ...
- R: 关于 table 函数的应用
################################################### 问题:关于 table 函数 18.5.9 来一个关于 table 函数的例子,说明tabl ...
- Java堆分析