char dict[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'}; class Solution { public: string pattern(int v, int level) { int offset = int(log10(level)) * 2; char *p = dict + offset; string ret; switch(v) { case 1: case 2: case 3: for(int i = 0; i < v; i++) ret += *p; break; case 4: ret += *(p); case 5: ret += *(p+1); break; case 6: case 7: case 8: ret += *(p+1); for(int i = 0; i < (v - 5); i ++) ret += *(p); break; case 9: ret += *(p); case 10: ret += *(p+2); break; } return ret; } string intToRoman(int num) { string ret; int base = 10000; while(num) { base /= 10; ret += pattern(num/base, base); if (base > 1) num %= base; if (base == 1) break; } return ret; } };