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;
}
};
념념
'2020 이전 > 리트코친놈&amp;기타문제' 카테고리의 다른 글
프로그래머스 단어 변환 (0) | 2019.09.20 |
---|---|
리트코드문제 25 Reverse Nodes in k-Group (0) | 2019.01.21 |
리트코드 문제 19 Remove n-th Node from End (0) | 2018.12.31 |
리트코드 문제 3 (0) | 2018.11.14 |