题目:
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;
}
};