LeetCode算法题之int to Roman
来源:互联网 发布:centos启动图形界面 编辑:程序博客网 时间:2024/05/19 18:14
问题描述:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
将数字转换为罗马数字表示。
解题思路:
按照罗马数字组数标准组合即可,附罗马数字百度百科:http://baike.baidu.com/link?url=UUF_EffApqqZ3Hh2K_7f8NOYpFYIAtk8oWQMWkmN1KnO3TOeiitCwubqKI_boYSlsKOEij4zrwMT0oGN4Ev0YK
class Solution{public: string intToRoman(int num) { map<int, char> Int_Roman; Int_Roman.insert(pair<int,char>(1,'I')); Int_Roman.insert(pair<int,char>(5,'V')); Int_Roman.insert(pair<int,char>(10,'X')); Int_Roman.insert(pair<int,char>(50,'L')); Int_Roman.insert(pair<int,char>(100,'C')); Int_Roman.insert(pair<int,char>(500,'D')); Int_Roman.insert(pair<int,char>(1000,'M')); string result; if(num<0 || num >3999) return ""; //确定个十百千位,他们相加起来正好等于num int single = num % 10; int ten = num % 100 - single; int hundred = num % 1000 - num % 100; int thousand = num - num % 100; //确定千位 for(int j=0; j<thousand/1000; j++ ) { result += Int_Roman[1000]; } //确定百位 if(hundred == 900) { result += "CM"; } else if(hundred == 400) { result += "CD"; } else if(hundred < 400) { for(int j=0; j<hundred/100; j++ ) { result += Int_Roman[100]; } } else { result += 'D'; for(int j=0; j<(hundred - 500)/100; j++ ) { result += Int_Roman[100]; } } //确定十位 if(ten == 90) { result += "XC"; } else if(ten == 40) { result += "XL"; } else if(ten < 40) { for(int j=0; j<ten/10; j++ ) { result += Int_Roman[10]; } } else { result += 'L'; for(int j=0; j<(ten - 50)/10; j++ ) { result += Int_Roman[10]; } } //确定个位 if(single == 9) { result += "IX"; } else if(single == 4) { result += "IV"; } else if(single < 4) { for(int j=0; j<single/1; j++ ) { result += Int_Roman[1]; } } else { result += 'V'; for(int j=0; j<(single - 5)/1; j++ ) { result += Int_Roman[1]; } }//..................................................... return result; }};
0 0
- LeetCode算法题之int to Roman
- LeetCode算法题之Roman to Integer
- Leetcode-12 int to Roman
- leetcode (12) - INT TO ROMAN
- leetcode (13) - ROMAN TO INT
- leetcode之Roman to Integer
- LeetCode之Roman to Integer
- leetcode 之 Roman to Integer
- leetCode 之 Roman to Integer
- 【Leetcode】之Integer to Roman
- 【Leetcode】之Roman to Integer
- leetcode之Roman to Integer
- LeetCode 之 Roman to Integer
- Leetcode 之 Roman to Integer
- Int to Roman And Roman to Int
- LeetCode OJ算法题(十二):Integer to Roman
- LeetCode OJ算法题(十三):Roman to Integer
- LeetCode算法题——12. Integer to Roman
- nodejs npm install 全局安装 非全局安装
- 简述REDO简单操作
- socket编程
- P51.17(输出2-n之间所有的素数。)
- 九宫格基本布局
- LeetCode算法题之int to Roman
- Android开发学习2-Handler
- 快速排序算法的C++实现
- c++面向对象程序设计第二章
- 我的java学习笔记(22)关于在框架中显式信息
- 第十五题
- 在安卓默认布局(RelativeLayout)中已经添加好好按钮,需要滚动条时如何添加?
- Java习惯用法总结
- Linux下的makefile编写详解