题解 | #牛牛和罗马数字智力游戏#
牛牛和罗马数字智力游戏
https://www.nowcoder.com/practice/7f4bd3b2d7d34f5c87d84120d9782c1d
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num int整型
* @param limit int整型
* @return string字符串
*/
vector<string> str{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
vector<int> divisor{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string integerToRomanWithReverse(int num, int limit) {
// write code here
string ans = "";
int index = 0;
while (num) {
int quotient = num / divisor[index];
while (quotient--) {
ans += str[index];
}
num %= divisor[index];
index++;
}
if (ans.size() >= limit) {
reverse(ans.begin(), ans.end());
}
return ans;
}
};
查看10道真题和解析