Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
罗马数字规则 http://wenku.baidu.com/view/e2b2f6797fd5360cba1adb19.html
#include
#include
using namespace std;
class Solution {
public:
string intToRoman(int num) {
string value="";
string unit="MDCLXVI";
int b=100;
int i=0;
int count=num/1000;
value.insert(0,count,'M');
num=num%1000;
while(b>0)
{
count=num/b;
switch(count)
{
case 0:
break;
case 1 ... 3:
value.insert(value.size(),count,unit[i+2]);
break;
case 4:// 5-1
value.append(&unit[i+2],1);
value.append(&unit[i+1],1);
break;
case 5 ... 8://5+0,1,2,3
value.append(&unit[i+1],1);
value.insert(value.size(),count-5,unit[i+2]);
break;
case 9://10-1
value.append(&unit[i+2],1);
value.append(&unit[i],1);
break;
}
num=num-count*b;
i=i+2;
b=b/10;
}
return value;
}
};
int main()
{
Solution s;
cout<<"5:"<
<
#include
#include
using namespace std;
class Solution {
public:
int romanToInt(string s) {
int value=0;
int i=0;
string unitC="MDCLXVI";
int unitI[]={1000,500,100,50,10,5,1,0};
int first=0;
int second=7;//初始化 while循环的第一次
while(i