本文主要是介绍Integer to Roman问题及解法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题描述:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
问题分析:
本题目目的是将一个整数转换为罗马数字的表现形式。我们可以将能用的到罗马数字存到数组里,然后再调用。
详见代码:
class Solution {
public:string intToRoman(int num) {string c[4][10]={{"","I","II","III","IV","V","VI","VII","VIII","IX"},{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},{"","M","MM","MMM"}};string roman;roman.append(c[3][num / 1000 % 10]);roman.append(c[2][num / 100 % 10]);roman.append(c[1][num / 10 % 10]);roman.append(c[0][num % 10]);return roman;}
};
题目是不是很有意思~
这篇关于Integer to Roman问题及解法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!