zoukankan      html  css  js  c++  java
  • Java [leetcode 12] Integer to Roman

    题目描述:

    Given an integer, convert it to a roman numeral.

    Input is guaranteed to be within the range from 1 to 3999.

    解题思路:

    这道题没什么技术含量,一个一个除10以及取模运算就行。

    代码如下:

    public class Solution {
        public String intToRoman(int num) {
    		String Roman[][] = {
    				{"", "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"}
    		};
    		int remainder;
    		int carry = num;
    		int index = 0;
    		String result = new String();
    		while(carry != 0){
    			remainder = carry % 10;
    			result = Roman[index][remainder] + result;
    			index++;
    			carry = carry / 10;
    		}
    		return result;
    	}
    }
    
  • 相关阅读:
    python中文编码
    Python习题纠错1
    Python中的变量
    Python之注释
    python初步学习
    java输入数据并排序
    五月最后一天
    @component注解
    多线程回顾
    赖床分子想改变--
  • 原文地址:https://www.cnblogs.com/zihaowang/p/4457001.html
Copyright © 2011-2022 走看看