Leetcode84: Integer to Roman
2015-09-29 10:46
274 查看
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Input is guaranteed to be within the range from 1 to 3999.
struct node{ int key; string szRoman; node(int k, string s):key(k), szRoman(s){} }; string intToRoman(int num){ vector<node> dct; dct.push_back(node(1000, "M")); dct.push_back(node(900, "CM")); dct.push_back(node(500, "D")); dct.push_back(node(400, "CD")); dct.push_back(node(100, "C")); dct.push_back(node(90, "XC")); dct.push_back(node(50, "L")); dct.push_back(node(40, "XL")); dct.push_back(node(10, "X")); dct.push_back(node(9, "IX")); dct.push_back(node(5, "V")); dct.push_back(node(4, "IV")); dct.push_back(node(1, "I")); string res; int i = 0; while(num > 0) { if(num/dct[i].key == 0) { i += 1; continue; } for(int j = 0; j < num/dct[i].key; ++j) res.append(dct[i].szRoman); num%=dct[i].key; } return res; }
相关文章推荐
- Linux设备驱动——字符设备驱动
- mysql 海量数据的存储和访问解决方案
- 更新包地址安装新版node.js
- PAT(甲级)1079
- LINUX CETOS7
- Eclipse安装swt插件
- JAVA代理模式Proxy Pattern
- Android自定义解析Html的TextView-HtmlView
- 将UITableView的横线设置满屏幕
- PAT(甲级)1078
- vsftp与mysql结合进行用户验证
- sql注入的问题,PreparedStatement
- 【TCO 2012】2A EvenPaths
- android学习各种bug(5)
- C语言 工具栏创建
- 工厂模式实现仓储模版
- 求最大公约数和最小公倍数
- iOS UI进阶-2.0 CALayer
- 基本数据结构(2)——算法导论(12)
- 素数表