[Leetcode] Integer to Roman

Source: Internet
Author: User

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&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.