您的位置:首页 > 其它

LeetCode *** 12. Integer to Roman

2016-04-11 20:14 281 查看
题目:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

分析:

i am a stupid girl。放张自拍。

代码:

class Solution {
public:
string intToRoman(int num) {

string res="";

int p[4]={0};
char symbol[7]={'I','V','X','L','C','D','M'};

for(int i=0;i<4&#++i){
p[i]=num%10;
num/=10;
}

for(int i=3;i>=0;--i){
int index=i*2;
if(p[i]!=0){
int high=p[i]/5;
int low=p[i]%5;
if(high&&low<4)res+=symbol[index+1];
if(low<4)for(int i=0;i<low;++i)res+=symbol[index];
if(low==4){
res+=symbol[index];
res+=symbol[index+1+high];
}

}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: