C + + integer converted to roman numerals (one)---"Those strange Algorithms" __web

Source: Internet
Author: User

reference: "One-day-one-leetcode"

requirement : Convert the plastic numbers into Roman numerals, we all know there are 7 Roman numerals, I (1), V (5), X (Ten), L (M), C (), D (+), M. (1000), which has the disadvantage that we need to limit our input in 1~ In 3999, the Roman numeral representation cannot be used because it is greater than 3999 or less than 1.

Code implementation:
1) _simple We enumerate all the possible situations, and then make the corresponding match, the code logic is simple;
2 The second kind we discuss according to the classification, the logic is relatively simple, but the code implementation is more redundant.

#include <iostream> #include <string> #include <vector> using namespace std;
    string inttoroman_simple (int num) {int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    String str = "";
        for (int i = 0; i < sizeof (values)/sizeof (values[0)); i++) {String str_t = "";
            while (Num >= values[i]) {num-= values[i];
        str_t + = Numerals[i];
    STR + + str_t;
return str;
    string inttoroman (int num) {string str = "";
    String Roman[7] = {"I", "V", "X", "L", "C", "D", "M"};
    int j = 0;
        while (num) {string str_t = "";
        int temp = num% 10;
            if (Temp < 4) {int count = temp;
            while (count--) {str_t + = Roman[j];
        } else if (temp = = 4) {str_t + = Roman[j];    str_t + = roman[j + 1];
            else if (temp >= 5 && temp <= 8) {str_t + = roman[j + 1];
            int count = temp-5;
            while (count--) {str_t + = Roman[j];
            } else{str_t + = Roman[j];
        str_t + = Roman[j + 2];
        str = str_t + str;
        cout << str_t << Endl;
        cout << str << Endl;
        cout << "==================" << Endl;
        Num/= 10;
    j = j + 2;

return str;
    int main () {int num = 3745;
    cout << inttoroman_simple (num) << Endl;
return 0; }

Run Result:

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.