LeetCode刷题(C++)——Integer to Roman(Medium)

来源:互联网 发布:c语言中putchar(10) 编辑:程序博客网 时间:2024/06/10 01:38

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

罗马数字:

1-9:{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }10-90:{"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }100-900:{"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }1000-3000:{"M", "MM", "MMM" }

class Solution {public:    string intToRoman(int num) {        string roman[][10] = { {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },{"","C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },{"", "M", "MM", "MMM" } };string str;int i = 0;while (num){int j = num % 10;str = roman[i++][j] + str;num = num / 10;}return str;    }};


1 0
原创粉丝点击