12. 整数转罗马数字/13.罗马数字转整数(签到题)

2018-11-05  本文已影响19人  Gunther17

整数转罗马数字

12. 整数转罗马数字

c++ code:AC 68ms 超50%
这题就是细心就可以,慢慢划分。

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<sstream>
#include<assert.h>
#include<math.h>


using namespace std;

class Solution {
public:
    string intToRoman(int num) {
        if (1 > num || num > 3999) return "";

        string res = "";
        while (num != 0)
        {
            if (num >=1000)
            {
                num -= 1000;
                res += "M";
                continue;
            }
            else if (num >=500)
            {
                if (num >= 900)
                {
                    res += "CM";
                    num -= 900;
                    continue;
                }
                else
                {
                    num -= 500;
                    res += "D";
                    continue;
                }

            }
            else if (num >= 100)
            {
                if (num >= 400)
                {
                    num -= 400;
                    res += "CD";
                    continue;
                }
                else
                {
                    num -= 100;
                    res += "C";
                    continue;
                }

            }
            else if (num >=50)
            {
                if (num >= 90)
                {
                    num -= 90;
                    res += "XC";
                    continue;
                }
                else
                {
                    num -= 50;
                    res += "L";
                    continue;
                }

            }
            else if (num >= 10)
            {
                if (num >= 40)
                {
                    num -= 40;
                    res += "XL";
                    continue;
                }
                else
                {
                    num -= 10;
                    res += "X";
                    continue;
                }

            }
            else if (num >= 5)
            {
                if (num >= 9)
                {
                    num -= 9;
                    res += "IX";
                    continue;
                }
                else
                {
                    num -= 5;
                    res += "V";
                    continue;
                }

            }
            else
            {
                if (num >= 4)
                {
                    num -= 4;
                    res += "IV";
                    continue;
                }
                else
                {
                    num -= 1;
                    res += "I";
                    continue;
                }

            }
        }
        return res;
    }
};

int stringToInteger(string input)
{
    return stoi(input);
}


int main() {
    string line;
    while (getline(cin, line))
    {
        int num = stringToInteger(line);
        string ret = Solution().intToRoman(num);
        string out = (ret);
        cout << out << endl;
    }

    return 0;
}

string function
题目:罗马数字转整数

13.罗马数字转整数

c++ code:AC 43%

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<sstream>
#include<assert.h>
#include<math.h>


using namespace std;

class Solution {
public:
    int romanToInt(string s) {
        if (s.empty()) return 0;
        int res = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s.substr(i, 1) == "M")
                res += 1000;            
            if (s.substr(i, 1) == "D")
                res += 500;
            if (s.substr(i, 1) == "C")
            {
                if (s.substr(i, 2) == "CD")
                {
                    res += 400; i++;
                }
                else if (s.substr(i, 2) == "CM")
                {
                    res += 900; i++;
                }
                else
                    res += 100;
            }
                
            if (s.substr(i, 1) == "L")
                res += 50;
            if (s.substr(i, 1) == "X")
            {
                if (s.substr(i, 2) == "XL")
                {
                    res += 40; i++;
                }
                else if (s.substr(i, 2) == "XC")
                {
                    res += 90; i++;
                }
                else
                res += 10;
            }

            if (s.substr(i, 1) == "V")
                res += 5;
            if (s.substr(i, 1) == "I")
            {
                if (s.substr(i, 2) == "IV")
                {
                    res += 4; i++;
                }
                else if (s.substr(i, 2) == "IX")
                {
                    res += 9; i++;
                }
                else 
                res += 1;
            }

        }

        return res;

    }
};

string stringToString(string input) {
    assert(input.length() >= 2);
    string result;
    for (int i = 1; i < input.length() - 1; i++) {
        char currentChar = input[i];
        if (input[i] == '\\') {
            char nextChar = input[i + 1];
            switch (nextChar) {
            case '\"': result.push_back('\"'); break;
            case '/': result.push_back('/'); break;
            case '\\': result.push_back('\\'); break;
            case 'b': result.push_back('\b'); break;
            case 'f': result.push_back('\f'); break;
            case 'r': result.push_back('\r'); break;
            case 'n': result.push_back('\n'); break;
            case 't': result.push_back('\t'); break;
            default: break;
            }
            i++;
        }
        else {
            result.push_back(currentChar);
        }
    }
    return result;
}

int main() {
    string line;
    while (getline(cin, line)) {
        string s = stringToString(line);

        int ret = Solution().romanToInt(s);

        string out = to_string(ret);
        cout << out << endl;
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读