Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num/1000!=0){ RomanDigit(sb,num/1000,"M","#","#"); num%=1000; } if(num/100!=0){ RomanDigit(sb,num/100,"C","D","M"); num%=100; } if(num/10!=0){ RomanDigit(sb,num/10,"X","L","C"); num%=10; } if(num!=0){ RomanDigit(sb,num,"I","V","X"); } return new String(sb); } private void RomanDigit(StringBuilder sb,int digit,String a,String b,String c){ switch(digit){ case 1:sb.append(a);return; case 2:sb.append(a+a);return; case 3:sb.append(a+a+a);return; case 4:sb.append(a+b);return; case 5:sb.append(b);return; case 6:sb.append(b+a);return; case 7:sb.append(b+a+a);return; case 8:sb.append(b+a+a+a);return; case 9:sb.append(a+c);return; } return; } }
int RomanDigit(char* roman,int digit,char a,char b,char c){ switch(digit){ case 1:roman[0]=a;return 1; case 2:roman[0]=a;roman[1]=a;return 2; case 3:roman[0]=a;roman[1]=a;roman[2]=a;return 3; case 4:roman[0]=a;roman[1]=b;return 2; case 5:roman[0]=b;return 1; case 6:roman[0]=b;roman[1]=a;return 2; case 7:roman[0]=b;roman[1]=a;roman[2]=a;return 3; case 8:roman[0]=b;roman[1]=a;roman[2]=a;roman[3]=a;return 4; case 9:roman[0]=a;roman[1]=c;return 2; } return 0; } char* intToRoman(int num) { //char digit[9][5]={I,II,III,IV,V,VI,VII,VIII,IX}; //char tensdig[9][5]={X,XX,XXX,XL,L,LX,LXX,LXXX,XC}; //char hunsdig[9][5]={C,CC,CCC,CD,D,DC,DCC,DCCC,CM}; //char thodig[3][3]={M,MM,MMM}; char* roman=(char*)malloc(sizeof(char)*16); int index=0; if(num/1000!=0){ index+=RomanDigit(roman+index,num/1000,'M','#','#'); num%=1000; } if(num/100!=0){ index+=RomanDigit(roman+index,num/100,'C','D','M'); num%=100; } if(num/10!=0){ index+=RomanDigit(roman+index,num/10,'X','L','C'); num%=10; } if(num!=0){ index+=RomanDigit(roman+index,num,'I','V','X'); } roman[index]=0; return roman; }
class Solution { public: string intToRoman(int num) { char* roman=(char*)malloc(sizeof(char)*16); int index=0; if(num/1000){ index+=RomanDigit(roman+index,num/1000,'M','#','#'); num%=1000; } if(num/100){ index+=RomanDigit(roman+index,num/100,'C','D','M'); num%=100; } if(num/10){ index+=RomanDigit(roman+index,num/10,'X','L','C'); num%=10; } if(num){ index+=RomanDigit(roman+index,num,'I','V','X'); } roman[index]=0; return string(roman); } private: int RomanDigit(char* s,int digit,char a,char b,char c){ switch(digit){ case 1:s[0]=a;return 1; case 2:s[0]=a;s[1]=a;return 2; case 3:s[0]=a;s[1]=a;s[2]=a;return 3; case 4:s[0]=a;s[1]=b;return 2; case 5:s[0]=b;return 1; case 6:s[0]=b;s[1]=a;return 2; case 7:s[0]=b;s[1]=a;s[2]=a;return 3; case 8:s[0]=b;s[1]=a;s[2]=a;s[3]=a;return 4; case 9:s[0]=a;s[1]=c;return 2; } return 0; } };
class Solution: # @param {integer} num # @return {string} def intToRoman(self, num): s='' if num/1000!=0: s=self.RomanDigit(s,num/1000,'M','#','#') num%=1000 if num/100!=0: s=self.RomanDigit(s,num/100,'C','D','M') num%=100 if num/10!=0: s=self.RomanDigit(s,num/10,'X','L','C') num%=10 if num!=0: s=self.RomanDigit(s,num,'I','V','X') return s def RomanDigit(self,s,digit,a,b,c): if digit<4: s+=a*digit return s elif digit==4: s+=a+b return s elif digit<9: s+=b+a*(digit-5) return s else: s+=a+c return s
LeetCode 12 Integer to Roman(C,C++,Java,Python)
原文地址:http://blog.csdn.net/runningtortoises/article/details/45578393