정수 Leetcode 전송 로마 숫자

제목 설명

정수를 감안할 때, 로마 숫자로 변환합니다. 입력이 1 내지 3999의 범위에 있도록한다.

사고

첫째, 문자열의 디지털 변환, 하나 개의 프로세스 하나, 함수 호출 캔 도움말

코드

class Solution {
public:
    string Help(string s, int i, int len)
    {
        string record = "IVXLCDMT";
        string res;
        int number = s[i] - '0';
        if (number>0)
        {
            int a = number / 5;
            int b = number % 5;
            if (b == 4)
            {
                res += record[(len - i) * 2 - 2];
                res += record[(len - i) * 2 - 1 + a];
            }
            else
            {
                if (a == 1)
                    res += record[(len - i) * 2 - 1];
                while (b > 0)
                {
                    res += record[(len - i) * 2 - 2];
                    b--;
                }
            }
        }
        return res;
    }

    string intToRoman(int num) {
        string res;
        string s = to_string(num);
        int len = s.length();
        for (int i = 0; i< len; i++)
        {
            res += Help(s, i, len);
        }
        return res;
    }
};
게시 85 개 원래 기사 · 원의 칭찬 0 · 조회수 372

추천

출처blog.csdn.net/weixin_38312163/article/details/105062446