Problem Description:
Given an integer, convert it to a Roman numeral.
Input is guaranteed to being within the range from 1 to 3999.
Basic ideas:
Familiar with the rules of Roman numerals and Arabic numerals, and find out the laws. Write the program. This problem focuses on the law.
The mapping of Arabic numerals to Roman numerals can be referred to Http://baike.baidu.com/view/42061.htm?fr=aladdin
Code:
String Toroman (string Roman, int size, int num)//c++ {string tmp = ""; if (num = = 0) return tmp; int tmpnum = num; int tmpsize = size; while (Tmpsize > 1) {tmpnum = TMPNUM/10; tmpsize--; } int topnum = Tmpnum; if (Tmpnum <4) {while (tmpnum>0) {tmp + = Roman.substr ((size-1) *2,1); tmpnum--; }} else if (Tmpnum > 8) {tmp + = Roman.substr ((size-1) *2,1); TMP +=roman.substr ((size-1) *2+2,1); } else {if (Tmpnum ==4) {tmp + = ROMAN.SUBSTR (size- 1) *2,1); TMP + = Roman.substr ((size-1) *2+1,1); } else {tmp + = Roman.substr ((size-1) *2+1,1); Tmpnum = tmpnum-5; while (tMPNUM>0) {tmp + = Roman.substr ((size-1) *2,1); tmpnum--; }}} tmpsize =size; while (Tmpsize >1) {topnum *= 10; tmpsize--; } tmp + = Toroman (roman,size-1,num-topnum); return TMP; } string inttoroman (int num) {string Roman = "IVXLCDM"; int size = 0; int temp = num; while (temp >0) {size++; temp = TEMP/10; The string result = Toroman (roman,size,num); return result; }
[Leetcode] Integer to Roman