标签:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
1 string intToRoman(int num) { 2 string res; 3 char a[4]={‘I‘,‘X‘,‘C‘,‘M‘}; 4 char b[3]={‘V‘,‘L‘,‘D‘}; 5 int i=3,ld=0,n=1000; 6 while(i>=0) 7 { 8 ld=num/n; 9 num%=n; 10 n/=10; 11 12 switch(ld) 13 { 14 case 0:break; 15 case 1:{res+=a[i];break;} 16 case 2:{res=res+a[i]+a[i];break;} 17 case 3:{res=res+a[i]+a[i]+a[i];break;} 18 case 4:{res=res+a[i]+b[i];break;} 19 case 5:{res=res+b[i];break;} 20 case 6:{res=res+b[i]+a[i];break;} 21 case 7:{res=res+b[i]+a[i]+a[i];break;} 22 case 8:{res=res+b[i]+a[i]+a[i]+a[i];break;} 23 case 9:{res=res+a[i]+a[i+1];break;} 24 default :break; 25 26 } 27 i--; 28 29 30 } 31 return res; 32 33 }
标签:
原文地址:http://www.cnblogs.com/hexhxy/p/4797888.html