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 ...
随机推荐
- [转]七个对我最好的职业建议(精简版)--Nicholas C. Zakas
一.不要别人点什么,就做什么 我的第一份工作,只干了8个月,那家公司就倒闭了.我问经理,接下来我该怎么办,他说: "小伙子,千万不要当一个被人点菜的厨师,别人点什么,你就烧什么.不要接受那样 ...
- bash批量处理
[root@azure_dbm1_s1 scripts]# more slave_1062_stop.sh #/bin/bash slave_num=`mysql -e "show slav ...
- 问题5:如何快速找到多个字典中的公共键(key)
方法一:for in循环 from random import randint, sample a1 = {k; randint(1, 4) for k in 'abcdefg'} a2 = {k; ...
- Servlet的生命周期以及简单工作原理的讲解
Servlet生命周期分为三个阶段: 1,初始化阶段 调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destr ...
- JavaScript接口
JavaScript中实现接口的方法有三种: 第一种,使用注释的方法实现接口 特点:(1)最简单,但是功能最弱(2)利用 interface和 implement"文字"(3)把他 ...
- IIS7.0(虚拟机)发布MVC5程序出现Http403错误的解决方法.
近来,用MVC5开发自己的一个小网站.网上租用了一个小空间(虚拟主机),可选.net版本为2.0 3.0 3.5 4.0 ,上传网站 后发现是403错误.不能访问. 经与技术人员联系,把虚拟机更换到高 ...
- Fast Walsh–Hadamard transform
考虑变换 $$\hat{A_x} = \sum_{i\ or\ x = x}{ A_i }$$ 记 $S_{t}(A,x) = \sum_{c(i,t)\ or\ c(x,t)=c(x,t),\ i ...
- javascript 数组中出现的次数最多的元素
javascript 数组中出现的次数最多的元素 var arr = [1,-1,2,4,5,5,6,7,5,8,6]; var maxVal = arr[0]; // 数组中的最大值 var min ...
- 开源库ActiveAndroid + gson使用
ActiceAndroid的简介 ActiveAndroid是一个活跃的记录风格的ORM(对象关系映射)库.ActiveAndroid可以让您保存和检索的SQLite数据库记录而没有写一个SQL语句. ...
- 利用Fitnesse测试外部jar包
1. 下载Fitnesse官方jar http://www.fitnesse.org/FitNesseDownload 2. 下载后,创建下面目录,其中FitnesseRoot目录,不需要创建,Fit ...