首页 > 试题广场 >

数字变成罗马数字

[编程题]数字变成罗马数字
  • 热度指数:8213 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
请将给出的整数转化为罗马数字
保证输入数字的范围在1 到 3999之间。
示例1

输入

1

输出

"I"
//感觉这道题没什么意义 
public String intToRoman(int num) {
		int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
		String[] strs = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < values.length; i++) {
			while (num >= values[i]) {
				num -= values[i];
				sb.append(strs[i]);
			}
		}
		return sb.toString();
	}

编辑于 2017-07-16 12:01:36 回复(1)
public static String intToRoman(int num) {
    
    String M[] = {"", "M", "MM", "MMM"};
    
    String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    
    String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    
    String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    
    return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
}

发表于 2017-03-12 23:59:02 回复(0)
public class Solution {
    public String intToRoman(int num) {
        StringBuilder sb = new StringBuilder();

        String[] signs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int[] values =   {1000,900,500, 400,100, 90,  50, 40,  10, 9,   5,  4,   1};
        for (int i = 0; num!=0; i++) {
            while (num>=values[i]){
                num -= values[i];
                sb.append(signs[i]);
            }
        }

        return sb.toString();
    }
}
发表于 2018-05-11 21:52:32 回复(0)
class Solution {
public:
    string intToRoman(int num) {
       string one[]={"I","II","III","IV","V","VI","VII","VIII","IX"};
       string ten[]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
       string hundred[]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
       string thou[]={"M","MM","MMM"};
       string roman="";
       if(num>=1000){roman += thou[num/1000-1]; num = num%1000;}
       if(num>=100){roman += hundred[num/100 -1]; num = num %100;}
       if(num>=10){roman += ten[num/10 - 1];num = num%10;}
       if(num>=1) roman += one[num%10 - 1];
       return roman;
    }
};

发表于 2016-08-20 09:54:57 回复(0)
string intToRoman(int num) {
        string res="";
        vector<int> key={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        vector<string> value={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        for(int i=0;i<key.size();++i){
            int count=num/key[i];
            for(int j=0;j<count;++j)
                res+=value[i];
            num=num%key[i];
        }
        return res;    
    }

发表于 2017-05-08 17:35:23 回复(0)
class Solution {
    public:
        vector> dict={
            {1,'I'},
            {10,'X'},
            {100,'C'},
            {1000,'M'},
        };
        vector> dict2={
            {5,'V'},
            {50,'L'},
            {500,'D'},
        };
        string intToRoman(int num) {
            int tmp;
            int n;
            string ret="";
            for(int i=3;i>=0;i--){
                n=num/dict[i].first;
                if(n>0){
                    if(1<=n&&n<=3)
                        ret=ret+string(n,dict[i].second);
                    if(n==4)
                        ret=ret+dict[i].second+dict2[i].second;
                    if(n==5)
                        ret=ret+dict2[i].second;
                    if(6<=n&&n<=8)
                        ret=ret+dict2[i].second+string(n-5,dict[i].second);
                    if(n==9)
                        ret=ret+dict[i].second+dict[i+1].second;
                    num=num%dict[i].first;
                }
            }
            return ret;
        }
    };
发表于 2020-07-22 14:31:46 回复(0)
2020.07.12
首先搞清楚罗马数字,然后枚举出符号
I    IV    V    IX    X    XL    L    XC    C    CD    D    CM    M    
1    4    5    9    10    40    50    90    100    400    500    900    1000
从后往前逐个比对就可以了
public String intToRoman (int num) {
        // write code here
        StringBuffer sb = new StringBuffer();
        int[] s = new int[]{1,4,5,9,10,40,50,90,100,400,500,900,1000};
        String[] st=new String[]{"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
        for(int i =s.length-1;i>=0;){
            if(num>=s[i]){
                sb=sb.append(st[i]);
                num-=s[i];
            }else{
                i--;
            }
        }
        return sb.toString();
    }


发表于 2020-07-12 12:33:46 回复(0)
首先了解罗马数字的基本符号:

I  1

V  5

X  10

L  50

C  100

D  500

M  1000

再加上特殊的:900,400,90,40,9,4


import java.util.*;


public class Solution {
    /**
     * 
     * @param num int整型 
     * @return string字符串
     */
    public String intToRoman (int num) {
        // write code here
        
        int[] numbers={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] str={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        String out="";
        
        for(int i=0 ; i<numbers.length;i++)
        {
            while(true)
            {
                if(num/numbers[i]>0)
                {
                     out=out+str[i];
                     num=num-numbers[i];
                }
                else
                    break;
            }           
        }
        
        return out;
        
    }
}


发表于 2020-07-06 13:50:37 回复(0)

思路

预先保存个、十、百、千位的各数值和对应的罗马表示符;
分离给定数字里的个位、十位、百位、千位;
然后将分离出的数字位与对应的罗马表示符进行匹配;
拼接个、十、百、千位的匹配字符串,形成最终的结果;

代码如下

public class Solution {
    public String intToRoman(int num) {
        Map<Integer, String> onesMap = new HashMap<>(10);
        Map<Integer, String> tensMap = new HashMap<>(10);
        Map<Integer, String> hundredsMap = new HashMap<>(10);
        Map<Integer, String> thousandsMap = new HashMap<>(3);

        // 准备数据
        // 个位
        onesMap.put(0, "");
        onesMap.put(1, "I");
        onesMap.put(2, "II");
        onesMap.put(3, "III");
        onesMap.put(4, "IV");
        onesMap.put(5, "V");
        onesMap.put(6, "VI");
        onesMap.put(7, "VII");
        onesMap.put(8, "VIII");
        onesMap.put(9, "IX");

        // 十位
        tensMap.put(0, "");
        tensMap.put(1, "X");
        tensMap.put(2, "XX");
        tensMap.put(3, "XXX");
        tensMap.put(4, "XL");
        tensMap.put(5, "L");
        tensMap.put(6, "LX");
        tensMap.put(7, "LXX");
        tensMap.put(8, "LXXX");
        tensMap.put(9, "XC");

        // 百位
        hundredsMap.put(0, "");
        hundredsMap.put(1, "C");
        hundredsMap.put(2, "CC");
        hundredsMap.put(3, "CCC");
        hundredsMap.put(4, "CD");
        hundredsMap.put(5, "D");
        hundredsMap.put(6, "DC");
        hundredsMap.put(7, "DCC");
        hundredsMap.put(8, "DCCC");
        hundredsMap.put(9, "CM");

        // 千位
        thousandsMap.put(0, "");
        thousandsMap.put(1, "M");
        thousandsMap.put(2, "MM");
        thousandsMap.put(3, "MMM");

        if (num < 10) {
            String result = onesMap.get(num);
            return result;
        }
        if (num < 100) {
            // 获取十位
            int tensPlace = num / 10;
            String tensStr = tensMap.get(tensPlace);
            // 获取个位
            int onesPlace = num - (tensPlace * 10);
            String onesStr = onesMap.get(onesPlace);
            // 拼接
            return tensStr + onesStr;
        }
        if (num < 1000) {
            // 获取百位
            int hundredsPlace = num / 100;
            String hundredsStr = hundredsMap.get(hundredsPlace);
            // 获取十位
            int tensPlace = (num - (hundredsPlace * 100)) / 10;
            String tensStr = tensMap.get(tensPlace);
            // 获取个位
            int onesPlace = num - (hundredsPlace * 100) - (tensPlace * 10);
            String onesStr = onesMap.get(onesPlace);
            return hundredsStr + tensStr + onesStr;
        }
        if (num < 4000) {
            // 获取千位
            int thousandsPlace = num / 1000;
            String thousandsStr = thousandsMap.get(thousandsPlace);
            // 获取百位
            int hundredsPlace = (num - (thousandsPlace * 1000)) / 100;
            String hundredsStr = hundredsMap.get(hundredsPlace);
            // 获取十位
            int tensPlace = (num - (thousandsPlace * 1000) - (hundredsPlace * 100)) / 10;
            String tensStr = tensMap.get(tensPlace);
            // 获取个位
            int onesPlace = num - (thousandsPlace * 1000) - (hundredsPlace * 100) - (tensPlace * 10);
            String onesStr = onesMap.get(onesPlace);
            return thousandsStr + hundredsStr + tensStr + onesStr;
        }
        else {
            return "not supported num!";
        }
    }
}
发表于 2019-07-30 18:38:42 回复(0)
class Solution {
public:
//表格对照查找
    string intToRoman(int num) {
        string one[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
        string ten[] = { "X","XX", "XXX","XL","L","LX", "LXX", "LXXX", "XC" };
        string hundred[] = { "C","CC", "CCC", "CD", "D","DC", "DCC", "DCCC", "CM" };
        string thou[] = { "M", "MM", "MMM" };
        string roman = "";
        if (num >= 1000) { 
            roman += thou[num / 1000 - 1];
            num = num % 1000; 
        }
        if (num >= 100) {
            roman += hundred[num / 100 - 1];
            num = num % 100;
        }
        if (num >= 10) {
            roman += ten[num / 10 - 1];
            num = num % 10;
        }
        if (num >= 1)
            roman += one[num % 10 - 1];
        return roman;
    }
};
发表于 2019-07-19 20:38:41 回复(0)

其实就是操作数组,然后拼接字符串的问题。

public class Solution {
    public String intToRoman(int num) {
    String ones[]={"I","II","III","IV","V","VI","VII","VIII","IX"};
    String tens[]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
    String bais[]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
    String qians[]={"M","MM","MMM"};
    String ge = num%10==0?"":ones[num%10 - 1];
    String shi = num/10%10==0?"":tens[num/10%10 - 1];
    String bai = num/100%10==0?"":bais[num/100%10 - 1];
    String qian = num/1000==0?"":qians[num/1000 - 1];
    return qian + bai + shi + ge;
    }
}


编辑于 2018-12-28 10:22:03 回复(0)
class Solution {
public:
    string intToRoman(int num) {
        string res = "";
        vector<int> key = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        vector<string> value={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        for(int i = 0;i < key.size(); i++){
            int count = num / key[i];
            for(int j = 0;j < count; j++){
                res += value[i];
            }
            num %= key[i];
        }
        return res;
    }
};

发表于 2018-09-19 18:30:05 回复(0)
class Solution { public: string intToRoman(int num) { vector<int> values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; vector<string> strs = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; string res; while(num > 0) { for(int i = 0; i < values.size(); ++i) { if(num >= values[i]) { res += strs[i]; num -= values[i]; break; } } } return res; } }; </string></int>
发表于 2018-09-17 21:01:52 回复(0)
class Solution {
public:
    string intToRoman(int num) {
        string ret = "";
        int key[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        string value[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        for(int i=0; i<13 && num>0; ++i){
            while(num >= key[i]){
                num -= key[i];
                ret += value[i];
            }
        }
        return ret;
    }
};
发表于 2018-08-12 21:18:24 回复(0)
class Solution {
public:
    char dict[3][3]={{'I','V','X'},{'X','L','C'},{'C','D','M'}};
    int nums[4]={1,10,100,1000};
    string intToRoman(int num) {

        string s="";
        for(int i=3;i>0&&num>0;i--)            
        {
            int tp=num/nums[i];
            int tpp=num%nums[i];
            for(int p=0;p<tp;p++)
                 s+=dict[i-1][2];
            num-=tp*nums[i];

            int k=tpp/nums[i-1];
            if(k>8)
            {
                 for(int p=0;p<10-k;p++)
                     s+=dict[i-1][0];
                 s+=dict[i-1][2];
            }
            else if(k>=5&&k<=8)
            {
                s+=dict[i-1][1];
                for(int p=0;p<k-5;p++)
                     s+=dict[i-1][0];
             }
             else if(k>3)
             {
                for(int p=0;p<k-3;p++)
                  s+=dict[i-1][0];
                 s+=dict[i-1][1];
             }
            else
            {
                for(int p=0;p<k;p++)
                  s+=dict[i-1][0];
            }
            num-=nums[i-1]*k;
        }
        return s;
    }
};
发表于 2018-06-08 10:35:05 回复(0)
    //6. integer-to-roman
    string intToRoman(int num) {
        //I(1), X(10), C(100), M(1000), V(5), L(50), D(500)
        map<int, string> myMap{ {0, ""}, {1, "I"}, {2, "II"}, {3, "III"}, {4, "IV"}, {5, "V"}, {6, "VI"}, {7,"VII"}, {8, "VIII"}, {9, "IX"},
            {10, "X"}, {20, "XX"}, {30, "XXX"}, {40, "XL"}, {50, "L"}, {60, "LX"}, {70,"LXX"}, {80, "LXXX"}, {90, "XC"},
            {100, "C"}, {200, "CC"}, {300, "CCC"}, {400, "CD"}, {500, "D"}, {600, "DC"}, {700,"DCC"}, {800, "DCCC"}, {900, "CM"},
            {1000, "M"}, {2000, "MM"}, {3000, "MMM"} };
        string re = "";
        int bit = 1000;
        while(num) {
            if(num / bit) {
                int temp = num / bit * bit;
                re += myMap[temp];
                num -= temp;
            }
            else {
                bit /= 10;
            }
        }
        return re;
    }

发表于 2018-05-17 14:16:31 回复(0)
public class Solution {
    public String intToRoman(int num) {
        int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
        String[] strs = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            while (num >= values[i]) {
                num -= values[i];
                sb.append(strs[i]);
            }
        }
        return sb.toString();
    }
}

发表于 2018-03-28 15:37:13 回复(0)
class Solution {
public:
    map<int, char> roman_digit_map = {
        { 1, 'I' },{ 5, 'V' },{ 10, 'X' },
        { 50, 'L' },{ 100, 'C' },{ 500, 'D' },
        { 1000, 'M' },
    };

    string intToRoman(int num) {
        string res;

        if (num == 0)
            return NULL;

        stack<int> s;
        int digit = 0;
        while (num != 0) {
            digit = num % 10;
            s.push(digit);
            num = num / 10;
        }

        int digit_num = s.size();
        while (!s.empty()) {
            if (s.top() == 0) {
                s.pop();
                digit_num--;
                continue;
            }
            else {
                res.append(numberToRoman(s.top(), pow(10, digit_num - 1)));
                s.pop();
                digit_num--;
            }
        }

        return res;
    }

private:
    string numberToRoman(int digit, int multiplier) {
        string res;
        switch (digit) {
        case 1:
            res.push_back(roman_digit_map.at(1 * multiplier));
            break;
        case 2:
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            break;
        case 3:
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            break;
        case 4:
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(5 * multiplier));
            break;
        case 5:
            res.push_back(roman_digit_map.at(5 * multiplier));
            break;
        case 6:
            res.push_back(roman_digit_map.at(5 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            break;
        case 7:
            res.push_back(roman_digit_map.at(5 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            break;
        case 8:
            res.push_back(roman_digit_map.at(5 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(1 * multiplier));
            break;
        case 9:
            res.push_back(roman_digit_map.at(1 * multiplier));
            res.push_back(roman_digit_map.at(10 * multiplier));
            break;
        }

        return res;
    }
};

发表于 2018-01-28 11:30:02 回复(0)
public class Solution {
    public String intToRoman(int num) {
        int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
        String[] strs = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            while (num >= values[i]) {
                num -= values[i];
                sb.append(strs[i]);
            }
        }
        return sb.toString();
    }
}

发表于 2017-11-29 19:11:49 回复(0)
public String intToRoman(int num) {
		Map<Integer, String> map = new HashMap<>();
		int[] keys = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
		String[] values = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
		for (int i = 0; i < keys.length; i++) {
			map.put(keys[i], values[i]);
		}
		String res = "";
		for (int i = 0; i < keys.length; i++) {
			int count = num/keys[i];
			for(int j=0;j<count;j++){
				res += map.get(keys[i]);
			}
			num %= keys[i];
		}
		return res;
	}

发表于 2017-03-31 16:20:55 回复(0)