leetcode No12. Integer to Roman
来源:互联网 发布:网络电视哪个型号好 编辑:程序博客网 时间:2024/06/06 01:21
Question:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
即把数字转换成罗马数字
Algorithm:
按照罗马数字的规则转换即可,wikipedia有详细解释
Accepted Code:
class Solution {public: string intToRoman(int num) { string s;// map<int,char> hash;// hash[1000] = 'M';// hash[500] = 'D';// hash[100] = 'C';// hash[50] = 'L';// hash[10] = 'X';// hash[5] = 'V';// hash[1] = 'I'; int n1=num/1000; while(n1) { s.push_back('M'); n1--; } num=num%1000; if(num>=900) { s.push_back('C'); s.push_back('M'); num = num-900; } else if(num>=500) { s.push_back('D'); num = num-500; } if(num>=400) { s.push_back('C'); s.push_back('D'); num=num-400; } else { int n2=num/100; while(n2) { s.push_back('C'); n2--; } num=num%100; } if(num>=90) { s.push_back('X'); s.push_back('C'); num = num-90; } else if(num>=50) { s.push_back('L'); num = num-50; } if(num>=40) { s.push_back('X'); s.push_back('L'); num=num-40; } else { int n3=num/10; while(n3) { s.push_back('X'); n3--; } num=num%10; } if(num>=9) { s.push_back('I'); s.push_back('X'); num = num-9; } else if(num>=5) { s.push_back('V'); num = num-5; } if(num>=4) { s.push_back('I'); s.push_back('V'); num=num-4; } else { int n4=num; while(n4) { s.push_back('I'); n4--; } } return s; }};
0 0
- LeetCode : No12 Integer to Roman
- leetcode No12. Integer to Roman
- No12&13 Integer and Roman
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- LeetCode: Integer to Roman
- LeetCode : Roman to Integer
- [Leetcode] Roman to Integer
- [Leetcode] Integer to Roman
- Leetcode: Integer to Roman
- Android的日夜间切换
- 在VS2012中创建一个MFC动态链接项目(.dll)后,编译出现一大堆莫名其妙的错误的解决方法
- tikz-示例-x轴坐标竖线刻度
- Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史
- 欧拉筛法
- leetcode No12. Integer to Roman
- hadoop基本操作命令
- showPopupWindow();,注册广播接收者,
- 测试基础之黑盒测试
- 提高项目6.4-编写main函数,调用上面三个函数
- weak修饰符
- xcode江湖录-第04章 风水宝地--界面生成器
- linux下mysql配置文件my.cnf详解【转】
- MySQL下载安装、配置与使用(win7x64)