一、思路
模拟题,没啥好说的。要啥函数做啥函数就行,大胆写。
二、代码
class Solution { public: string intToRoman(int num) { string res; int a[4]; for(int i = 0; i < 4; i ++){ a[i] = num % 10; num /= 10; } for(int i = 0; i < a[3]; i ++) res += 'M'; res += create(a[2],"C","D","M"); res += create(a[1],"X","L","C"); res += create(a[0],"I","V","X"); return res; } string create(int num, string A, string B, string C){ if(num == 0) return ""; -- num;//菜鸡搞错了对应关系hh 亡羊补牢一下 懒得改了 string Roman[10]; Roman[0] = A; Roman[1] = Roman[0] + A; Roman[2] = Roman[1] + A; Roman[3] = Roman[0] + B; Roman[4] = B; Roman[5] = Roman[4] + A; Roman[6] = Roman[5] + A; Roman[7] = Roman[6] + A; Roman[8] = Roman[0] + C; return Roman[num]; } };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)