12.整数转罗马数字
Posted thefatcat
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.整数转罗马数字相关的知识,希望对你有一定的参考价值。
题目描述:
解法:
贪心法
class Solution { public: string intToRoman(int num) { vector<int> number = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; vector<string> roman={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; string result; for(int i =0;i<roman.size();i++){ while(num>=number[i]){ //注意=,以及这里需要用while,不能用if result.append(roman[i]); num -= number[i]; } } return result; } };
以上是关于12.整数转罗马数字的主要内容,如果未能解决你的问题,请参考以下文章