[Leetcode]12.integer to Roman

Source: Internet
Author: User

"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

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.