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 ...
随机推荐
- [IBM]掌握Ajax,Ajax中的高级请求和响应
掌握 Ajax, Ajax 中的高级请求和响应 http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html http://www.ibm.c ...
- zabbix发送邮件
1.zabbix服务器上已安装postfix邮件服务,如果没安装用yum安装sendmail也可以(简单) 2.vim /etc/mail.rc 在此配置中加上用户名及密码等,即可用这个账号发送邮件 ...
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- POJ百练—IP地址转换
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ]; void ...
- 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条
目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...
- Dubbo注册中心的四种配置方式详解
Dubbo目前支持4种注册中心,(multicast,zookeeper,redis,simple) 推荐使用Zookeeper注册中心. 一.Multicast注册中心 不需要启动任何中心节点,只要 ...
- css基础知识一
1.CSS (Cascding Style Sheet)层叠样式表 级联样式表 样式表 2.CSS作用: 修改页面中元素的显示样式 能够实现内容与表现的分离 提高代码的可重用性和可维护性 3.导入CS ...
- canvas实现平铺
代码: /** * Created by Administrator on 2016/1/30. */ function draw(id){ var canvas = document.getElem ...
- 树莓派 Learning 002 装机后的必要操作 --- 05 给树莓派搭建“x86 + pi”环境 -- 安装**32位运行库** -- 解决`E:未发现软件包 xxx` 问题
树莓派 装机后的必要操作 - 给树莓派搭建"x86 + pi"环境 – 安装32位运行库 – 解决E:未发现软件包 xxx 问题 我的树莓派型号:Raspberry Pi 2 Mo ...
- Sharepoint2013商务智能学习笔记之使用Current User Filter筛选Excel 数据(六)
Sharepoint自带的filter可以和Excel Web Access互动,下面将制作一个Demo,使用Current User Filter根据当前登录用户自动筛选Excel. 第一步,用Ex ...