"title"
Given an integer, convert it to a Roman numeral.
Input is guaranteed to being within the range from 1 to 3999.
"Analysis"
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
There are some special: there is a subtraction representation between each of the two stages, such as 900=CM, and C is written in front of M to indicate M-c.
Find the number of each Roman text (e.g. 3999/1000 = 3 results with 3 m)
"Code"
/********************************** Date: 2015-01-21* sjf0115* title: 12.Integer to roman* website: https://oj.leetcode.com/pr oblems/integer-to-roman/* Result: ac* Source: leetcode* Blog: **********************************/#include <iostream>using Namespace Std;class Solution {public:string inttoroman (int num) {string result; String roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int value[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; int count; Convert to Roman numerals for (int i = 0;i < 13;++i) {count = Num/value[i]; Result + = Toroman (Count,roman[i]); num = num% value[i]; }//if return result; }private:string toroman (int num,string str) {string result; for (int i = 0;i < Num;++i) {result + = str; }//for return result; }};int Main () {solution solution; int num = 3999; string result = Solution.inttoroman (num); Output cout<<result<<endl; return 0;}
[Leetcode]12.integer to Roman