zoukankan      html  css  js  c++  java
  • 力扣(LeetCode)试题12-整数转罗马数字 C++代码

    这道题写的,思路清奇?代码太臃肿了

    也算是有一丢丢特点,拿出来分享一下

    class Solution {
    public:
        string intToRoman(int num)
        {
            stack<char> temp;
            int count = 0;
            while (num)
            {
                ++count;
                int r = num % 10;
                num /= 10;
                if (count == 1)
                {
                    if (r <= 3)
                    {
                        int k = r;
                        while (k--) temp.push('I');
                    }
                    if (r == 4)
                    {
                        temp.push('V');
                        temp.push('I');
                    }
                    if (r == 5) temp.push('V');
                    if (r>5 && r <= 8)
                    {
                        int res = r - 5;
                        while (res--)
                        {
                            temp.push('I');
                        }
                        temp.push('V');
                    }
                    if (r == 9)
                    {
                        temp.push('X');
                        temp.push('I');
                    }
                }
                if (count == 2)
                {
                    if (r <= 3)
                    {
                        int k = r;
                        while (k--) temp.push('X');
                    }
                    if (r == 4)
                    {
                        temp.push('L');
                        temp.push('X');
                    }
                    if (r == 5) temp.push('L');
                    if (r>5 && r <= 8)
                    {
                        int res = r-5;
                        while (res--)
                        {
                            temp.push('X');
                        }
                        temp.push('L');
                    }
                    if (r == 9)
                    {
                        temp.push('C');
                        temp.push('X');
                    }
                }
                if (count == 3)
                {
                    if (r <= 3)
                    {
                        int k = r;
                        while (k--) temp.push('C');
                    }
                    if (r == 4)
                    {
                        temp.push('D');
                        temp.push('C');
                    }
                    if (r == 5) temp.push('D');
                    if (r>5 && r <= 8)
                    {
                        int res = r - 5;
                        while (res--)
                        {
                            temp.push('C');
                        }
                        temp.push('D');
                    }
                    if (r == 9)
                    {
                        temp.push('M');
                        temp.push('C');
                    }
                }
                if (count == 4)
                {
                    int k = r;
                    while (k--) temp.push('M');
                }
            }
            string str = "";
            while (!temp.empty())
            {
                str += temp.top();
                temp.pop();
            }
            return str;
        }
    };

  • 相关阅读:
    Java导出Excel(附完整源码)
    Scala语言学习笔记——方法、函数及异常
    python时间序列按频率生成日期
    socket主要函数介绍
    JAVA版CORBA程序
    Linux文件压缩命令笔记
    使用JAVA写一个简单的日历
    Node.js 文件系统
    Node.js 常用工具util
    Node.js 全局对象
  • 原文地址:https://www.cnblogs.com/pgzhanglin/p/13628183.html
Copyright © 2011-2022 走看看