1

可能重复:
如何将整数值转换为罗马数字字符串?

第一次在这里问问题。我有一个项目即将完成,我一直在做一些工作,但似乎在这里或其他地方找不到类似的问题。目标是接受没有上限的整数,并将其转换为罗马数字。由于整数实际上确实有一个上限,因此我必须在解析它之前将其转换为字符串,使用撇号来表示放置在子字符串中的每组三个字符。我在概念化循环时遇到了麻烦,a)根据他们的位置将罗马数字分配给他们所看到的 b)通过显示撇号来计算每组三个。

到目前为止,我有:

for(int i=0;i<(int)input.length()/3;i++){
    temp=input.substr(i,3);
    for(int j = 0; j < (int)temp.length(); j++){
        if(j == 0){
            if(temp[j] == '9') cout<<"CM";
            else if(temp[j] >= '8') cout<<"DCCC";
            else if(temp[j] >= '7') cout<<"DCC";
            else if(temp[j] >= '6') cout<<"DC";
            else if(temp[j] >= '5') cout<<"D";
            else if(temp[j] == '4') cout<<"CD";
            else if(temp[j] == '3') cout<<"CCC";
            else if(temp[j] == '2') cout<<"CC";
            else if(temp[j] == '1') cout<<"C";  
        }
        else if(j == 1){
            if(temp[j] == '9') cout<<"XC";
            else if(temp[j] >= '8') cout<<"LXXX";
            else if(temp[j] >= '7') cout<<"LXX";
            else if(temp[j] >= '6') cout<<"LX";
            else if(temp[j] >= '5') cout<<"L";
            else if(temp[j] == '4') cout<<"XL";
            else if(temp[j] == '3') cout<<"XXX";
            else if(temp[j] == '2') cout<<"XX";
            else if(temp[j] == '1') cout<<"X";
        }
        else if(j ==2){
            if(temp[j] == '9') cout<<"IX";
            else if(temp[j] == '8') cout<<"VIII";
            else if(temp[j] == '7') cout<<"VII";
            else if(temp[j] == '6') cout<<"VI";
            else if(temp[j] >= '5') cout<<"V";
            else if(temp[j] == '4') cout<<"IV";
            else if(temp[j] == '3') cout<<"III";
            else if(temp[j] == '2') cout<<"II";
            else if(temp[j] == '1') cout<<"I";
        }
    }
}

数字本身显示得很好,但我无法弄清楚如何告诉循环从右边开始,并以三分之二的方式工作,保持输入中数字的实际位置(例如 1234 应该将 1 显示为 I,而不是 C。我还需要弄清楚要在撇号中写入的循环。

4

2 回答 2

1

我能想到的转换为罗马数字的最简单方法是从最大可能的数字/组合开始检查并向下工作。包括组合,并从大到小检查它们,这样说“XC”总是在“L”之前检查,你不必担心“LXXXX”和“LXL”等。

// This code requires C++11 support.  Namely, initializer lists and type inference.
// If your compiler sucks, there's equivalents for the important stuff.  What really
// really matters is the order of the value=>digits mappings, and the iteration over
// them in the for loop.

#include <vector>
#include <string>
#include <utility>

std::string romanNumeralFor(int n, int markCount = 0) {
    typedef std::pair<int, std::string> valueMapping;
    static std::vector<valueMapping> importantNumbers = {
        {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"},
    };

    std::string result;
    bool needMark = false;
    std::string marks(markCount, '\'');
    for (auto mapping : importantNumbers) {
        int value = mapping.first;
        std::string &digits = mapping.second;
        while (n >= value) {
            result += digits;
            n -= value;
            needMark = true;
        }
        if ((value == 1000 || value == 100 || value == 10 || value == 1) && needMark) {
            result += marks;
            needMark = false;
        }
    }
    return result;
}

至于将字符串转换为数字:

// in C++11
int n = std::stoi(str);

// in C++03
std::istringstream iss(str);
int n;
iss >> n;

因此,将您的字符串分成三位数的块(从末尾开始!),并使用适当的标记计数传递它们。

于 2012-09-07T19:11:33.847 回答
0

不是最好的解决方案,但它有效。

#include <iostream>
#include <map>
#include <algorithm>
#include <string>

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
    if(from.empty())
        return;
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length();
    }
}

int romanNumeralToInt(std::string s) {
    std::map<char,int> romanNum = {
        {'I',1}, {'V',5}, {'X',10}, {'L',50}, {'C',100},
        {'D',500}, {'M',1000}
    };
    //{"IIIII",5}, {"VV",10}, {"XXXXX",50}, {"LL",100}, {"CCCCC",500}, {"DD",1000}
    int g = 0;
    std::sort(s.begin(),s.end());
    if(s.find("IIIII") != std::string::npos)
        replaceAll(s,"IIIII","V");
    if(s.find("VV") != std::string::npos)
        replaceAll(s,"VV","X");
    if(s.find("XXXXX") != std::string::npos)
        replaceAll(s,"XXXXX","L");
    if(s.find("LL") != std::string::npos)
        replaceAll(s,"LL","C");

    for(auto& i : s) {
        if(romanNum[i] != 0)
            g += romanNum[i];
    }
    return g;
}

int main() {
    std::string st = "XXXXXIIIIIVVVXLLD";
    int a = romanNumeralToInt(st);
    std::cout << a;
}

打印出 680。

于 2012-09-07T18:53:37.380 回答