LeetCode:Integer to Roman

来源:互联网 发布:单词记忆方法知乎 编辑:程序博客网 时间:2024/05/24 02:08

Given an integer, convert it to a roman numeral.

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

// Source : https://oj.leetcode.com/problems/integer-to-roman/// Author : Chao Zeng// Date   : 2014-12-21struct node {    int key;    string roman;    node(int k, string s):key(k),roman(s){}};class Solution{public:    string intToRoman(int num){        vector<node> value;        value.push_back(node(1000,"M"));        value.push_back(node(900,"CM"));        value.push_back(node(500,"D"));        value.push_back(node(400,"CD"));        value.push_back(node(100,"C"));        value.push_back(node(90,"XC"));        value.push_back(node(50,"L"));        value.push_back(node(40,"XL"));        value.push_back(node(10,"X"));        value.push_back(node(9,"IX"));        value.push_back(node(5,"V"));        value.push_back(node(4,"IV"));        value.push_back(node(1,"I"));        string result;        for (int i = 0; i < 13; i++){            while (num >= value[i].key){                    num -= value[i].key;                    result.append(value[i].roman);            }        }        return result;    }};


0 0