2020 이전/리트코친놈&기타문제

리트코드 문제 12 Integer to Roman

이상해C++ 2019. 1. 11. 22:13


class Solution {

public:

    string intToRoman(int num) {

    

        string str;

        int iThousand = num/1000;

        int iHundred = (num%1000)/100;

        

        for(int i=0;i<iThousand;i++)

        {

            str.append(1,'M');

        }

        

        if(iHundred ==4)

        {

            str.append("CD");

        }

        else if(iHundred == 9)

        {

            str.append("CM");

        }

        else

        {

            if(iHundred>=5)

            {

                str.append(1,'D');

                iHundred -=5;

            }

            for(int i=0; i<iHundred;i++)

            {

                str.append(1,'C');

            }

        }

        //ten

        int iTen = (num%100)/10;

        

        if(iTen ==4)

        {

            str.append("XL");

        }

        else if(iTen == 9)

        {

            str.append("XC");

        }

        else

        {

            if(iTen>=5)

            {

                str.append(1,'L');

                iTen -=5;

            }

            for(int i=0; i<iTen;i++)

            {

                str.append(1,'X');

            }

        }

        //one

        int iOne = num%10;

        

        if( iOne ==4)

        {

            str.append("IV");

        }

        else if( iOne == 9)

        {

            str.append("IX");

        }

        else

        {

            if( iOne>=5)

            {

                str.append(1,'V');

                iOne -=5;

            }

            for(int i=0; i< iOne;i++)

            {

                str.append(1,'I');

            }

        }

        return str;

    }

};


념념