码迷,mamicode.com
首页 > 其他好文 > 详细

Integer to Roman

时间:2015-09-10 15:45:09      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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     }

 

Integer to Roman

标签:

原文地址:http://www.cnblogs.com/hexhxy/p/4797888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!