【LeetCode解答六】Integer to Roman问题Java解答
2017-11-02 19:24
549 查看
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
以上是题目要求
想做这道题必须要明白罗马数字是怎么写的
这样就是1-1000的表示方式,题目要求最多输入3999,所以这就够用了,可以写代码了
Input is guaranteed to be within the range from 1 to 3999.
以上是题目要求
想做这道题必须要明白罗马数字是怎么写的
阿拉伯数字 | 罗马数字 |
1 | I |
4 | IV |
5 | V |
9 | IX |
10 | X |
40 | XL |
50 | L |
90 | XC |
100 | C |
400 | CD |
500 | D |
900 | CM |
1000 | M |
package Q12IntegerToRoman; import java.util.Scanner; /** * @author 单继重 * @since 2017/11/2 18:45 */ public class Solution { public static String intToRoman(int num) { String str = ""; String[] strings = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; int[] values = {1,4,5,9,10,40,50,90,100,400,500,900,1000}; for (int i = 12 ; num !=0 ; i --) { while (num >= values[i]) { num -= values[i]; str += strings[i]; } } return str; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); System.out.println(intToRoman(num)); } }
相关文章推荐
- 【LeetCode解答七】Roman to Integer问题Java解答
- leetcode_13题——Roman to Integer(string,数学问题)
- LeetCode 13 Roman to Integer (C,C++,Java,Python)
- leetcode题解||Integer to Roman问题
- 【LeetCode-面试算法经典-Java实现】【012-Integer to Roman(数字转罗马字符)】
- leetcode题解 || Roman to Integer问题
- [LeetCode] Roman to Integer || Integer to Roman (JAVA)
- 【leetcode】Roman to Integer【java】
- Roman To Integer leetcode java
- LeetCode 12 Integer to Roman(C,C++,Java,Python)
- [Leetcode] Roman to Integer (Java)
- 【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】
- Integer to Roman leetcode java
- [leetcode-12]Integer to Roman(java)
- [LeetCode][Java] Integer to Roman
- LeetCode 12 — Integer to Roman(C++ Java Python)
- leetcode_12题——Integer to Roman(string,数学问题)
- LeetCode OJ --问题与解答 Roman to Integer
- 【JAVA、C++】LeetCode 013 Roman to Integer
- LeetCode第13题--Roman to Integer(Java实现)