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;
        }
    };

  • 相关阅读:
    #Hadoop集群部署
    #Linux Keepalived 负载均衡
    #Linux LVS 负载均衡
    #Kafka 彻底删除topic
    #Linux Keepalived 双机热备
    Python #图片验证码
    Linux #tar
    MongoDB #$set的问题
    Django #CSRF
    keystone环境搭建(源码方式+yum方式)(ocata版本)
  • 原文地址:https://www.cnblogs.com/pgzhanglin/p/13628183.html
Copyright © 2011-2022 走看看