列表

详情


NC361. 整数转罗马数字

描述

给定一个正整数 n ,请你把这个数字转成罗马数字。罗马数字包含七种字符:'I' ,'V' ,'X' ,'L' ,'C' ,'D' ,'M' 分别表示 1 ,5,10,50,100,500,1000。
罗马数字记数的方法:
(1)相同的数字连写,所表示的数等于这些数字相加得到的数,如, Ⅲ = 3;
(2)小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如,Ⅷ = 8,Ⅻ = 12;
(3)小的数字在大的数字的左边,所表示的数等于大数减小数得到的数,如,Ⅳ = 4,Ⅸ = 9 。这个规则只适用于六个情况 I 可以放到 V 和 X 左边,X 可以放到 L 和 C 左边,C 可以放到 D 和 M 的左边

数据范围:

示例1

输入:

5

输出:

"V"

示例2

输入:

6

输出:

"VI"

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-07-31

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串
     */
    string ArabicToRoman(int n) {
        // write code here
        string temp = "";
        pair<int, string> pairVec[] = {
        {1000, "M"},
        {900,  "CM"},
        {500,  "D"},
        {400,  "CD"},
        {100,  "C"},
        {90,   "XC"},
        {50,   "L"},
        {40,   "XL"},
        {10,   "X"},
        {9,    "IX"},
        {5,    "V"},
        {4,    "IV"},
        {1,    "I"}
        };
        for(auto iter:pairVec)
        {
            while(iter.first<=n)
            {
                n = n - iter.first;
                temp+=iter.second;
            }
            if(n==0)
            {
                break;
            }
        }
        return temp;
    }
};

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-07-26

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串
     */
    string ArabicToRoman(int n) {
        // write code here
    static const char* ws[] = {"I", "IV", "V", "IX","X", "XL",
                          "L","XC", "C", "CD", "D", "CM", "M"};
    static const int ns[] =    {1,    4,   5,   9,   10,  40,   50, 90,  100,  400, 500, 900, 1000};
    string res; 
    int idx = 12;
    while(n) {
      if(n >= ns[idx]) {
        //cout << n << " " << ns[idx] <<" " << ws[idx] << endl;
        res.append(ws[idx]);
        n -= ns[idx];
      } else {
        idx --;
      }
    }
      return res;
    }
};

C 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-07-07

const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
const char* symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
char* ArabicToRoman(int n) {
    char* roman = malloc(sizeof(char) * 16);
    roman[0] = '\0';
    for (int i = 0; i < 13; i++) {
        while (n >= values[i]) {
            n -= values[i];
            strcpy(roman + strlen(roman), symbols[i]);
        }
        if (n == 0) {break;}
    }
    return roman;
}

C 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2022-03-20

const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
const char* symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

char* ArabicToRoman(int num) {
    // write code here
    char* roman = malloc(sizeof(char) * 16);
    roman[0] = '\0';
    for (int i = 0; i < 13; i++) {
        while (num >= values[i]) {
            num -= values[i];
            strcpy(roman + strlen(roman), symbols[i]);
        }
        if (num == 0) {
            break;
        }
    }
    return roman;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 464KB, 提交时间: 2022-08-02

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串
     */
    string ArabicToRoman(int n) {
        // write code here
        int num=n;
            vector<vector<string> > c={
            {"","I","II","III","IV","V","VI","VII","VIII","IX"},
            {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
            {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
            {"","M","MM","MMM"}
        };
        string roman;
        roman.append(c[3][num / 1000 % 10]);
        roman.append(c[2][num / 100 % 10]);
        roman.append(c[1][num / 10 % 10]);
        roman.append(c[0][num % 10]);
         
        return roman;
    }
};

上一题