标签:
#include<iostream>
#include<string>
using namespace std;
string intToRoman(int num) {
string str_result;
int temp=num;
if(temp>=1000)
{
int k=temp/1000;
for(int i=1;i<=k;i++)
str_result.push_back(‘M‘);
temp=temp%1000;
}
if(temp>=900)
{
str_result.push_back(‘C‘);
str_result.push_back(‘M‘);
temp=temp%900;
}
if(temp>=500)
{
str_result.push_back(‘D‘);
temp=temp%500;
}
if(temp>=400)
{
str_result.push_back(‘C‘);
str_result.push_back(‘D‘);
temp=temp%400;
}
if(temp>=100)
{
int k=temp/100;
for(int i=1;i<=k;i++)
str_result.push_back(‘C‘);
temp=temp%100;
}
if(temp>=90)
{
str_result.push_back(‘X‘);
str_result.push_back(‘C‘);
temp=temp%90;
}
if(temp>=50)
{
str_result.push_back(‘L‘);
temp=temp%50;
}
if(temp>=40)
{
str_result.push_back(‘X‘);
str_result.push_back(‘L‘);
temp=temp%40;
}
if(temp>=10)
{
int k=temp/10;
for(int i=1;i<=k;i++)
str_result.push_back(‘X‘);
temp=temp%10;
}
if(temp==9)
{
str_result.push_back(‘I‘);
str_result.push_back(‘X‘);
}
if(temp>=5&&temp<=8)
{
str_result.push_back(‘V‘);
temp=temp-5;
}
if(temp==4)
{
str_result.push_back(‘I‘);
str_result.push_back(‘V‘);
}
if(temp>=1&&temp<4)
{
for(int i=1;i<=temp;i++)
str_result.push_back(‘I‘);
}
return str_result;
}
int main()
{
int n=90;
cout<<intToRoman(n)<<endl;
system("pause");
return 1;
}
leetcode_12题——Integer to Roman(string,数学问题)
标签:
原文地址:http://www.cnblogs.com/yanliang12138/p/4441835.html