思路:
1.找出I/V/X/L/C/D/M组合规律,进行拼接;
1 4 5 9 10 40 50 90 100 400 500 900 1000.。。。
class Solution {
public:
string intToRoman(int num) {
int thresholds[7] = {1,5,10,50,100,500,1000};
char presents[7] = {'I','V','X','L','C','D','M'};
string s="";
int step = 6;
double tempLeft = 0;
int temp = 0,pos = 0;
while(step>=0 && num>=0){
temp = num / thresholds[step];
while(temp-->0) s = s + presents[step];
num %= thresholds[step];
tempLeft= num*1.0/thresholds[step];
pos = ((step+1)/2-1)*2;
if((tempLeft>=0.9 && !(step%2) ) && step>0){
s=s+presents[pos]+presents[step];
num -= 0.9*thresholds[step];
step--;
}else if(( (tempLeft>=0.8 && (step%2) ) ) && step>0){
s=s+presents[pos]+presents[step];
num -= 0.8*thresholds[step];
}
step--;
}
return s;
}
};