Leetcode 12th -- integer to Roman

Source: Internet
Author: User

Problem:

Given an integer, convert it to a roman numeral.

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

Converts an Arabic number to a Roman number. Baidu's corresponding I V x L C D m, representing 100,500,100, and then writing a sub-function, entering a number and the corresponding number level, such as Level 1, the value is 4. because it cannot exceed four thousand at most. So you can do the following. Note that the string usage is good. You can simply add it.

Class solution {PRIVATE: String corroman (INT Val, int level) {string base, media, large; string S = ""; if (Level = 1) // single digit {base = "I"; media = "v"; large = "X";} else if (Level = 2) {base = "X "; media = "L"; large = "C";} else if (Level = 3) {base = "C"; media = "D "; large = "M" ;}else {base = "M" ;}if (val = 0) Return "; if (Val <4) {for (INT I = 0; I <val; I ++) S + = base; return s;} If (val = 4) {return base + media ;} if (Val <9) {S = media; For (INT I = 5; I <val; I ++) S + = base; return s ;} return base + large;} public: String inttoroman (INT num) {string s; int A; For (INT I = 4; I> 0; I --) {A = num/POW (10, I-1); num % = (INT) Pow (10, I-1); s + = corroman (A, I );} return s ;}};

In this way, accept is enabled.

Leetcode 12th -- 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.