Integer to Roman
来源:互联网 发布:centos 6.5搭建lnmp 编辑:程序博客网 时间:2024/05/18 18:46
题目:Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
罗马数字有自己的组合规则,最简单的方法就是将其1-9,
10-90,100-900,1000-3000的写法放在数组中,通过判断
数字调用数组中的元素组合起来就是想要的罗马数字。
public static String intToRoman(int num) {String c[][] = {{ "0", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },{ "0", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },{ "0", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },{ "0", "M", "MM", "MMM" } };int tmp = num;StringBuffer st = new StringBuffer();if(tmp/1000!=0)st.append(c[3][tmp/1000]);if(tmp%1000/100!=0)st.append(c[2][tmp%1000/100]);if(tmp%100/10!=0)st.append(c[1][tmp%100/10]);if(tmp%10!=0)st.append(c[0][tmp%10]);return st.toString();}
更简单的方法是:
public static String intToRoman(int num) {String c[][] = {{ "0", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },{ "0", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },{ "0", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },{ "0", "M", "MM", "MMM" } };int digit = 0,temp=0;String st="";while(num!=0){temp=num%10;st=c[digit][temp]+st;digit++;num/=10;}return st;}
0 0
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- Integer to Roman and Roman to Integer
- LeetCode Integer to Roman & Roman to Integer
- Roman to Integer/Integer to Roman
- leetcode - roman to integer & integer to roman
- "Roman to Integer" and "Integer to Roman"
- Roman to Integer &&Integer to Roman
- Roman to Integer and Integer to Roman
- Roman to Integer/Integer to Roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- [leetcode] Product of Array Except Self
- Centos环境下安装Tomcat
- 欢迎使用CSDN-markdown编辑器
- Android Studio相关资源
- (4.3.1.7)【项目一】子Fragment之对话框
- Integer to Roman
- 打开套有 ListVew的 ScrollView的页面布局 默认 起始位置不是最顶部
- linux 任务管理器(top ps)
- [Android]Android接微信支付接口-如何跑通微信支付的demo (欢迎入坑) 群:121382044
- 使用release报错arc forbids explicit message send of 'release'
- Android与IIS身份验证——基本验证_HttpClient Http请求 验证
- LeetCode之Roman & Integer 的转换(简单题)
- 文章标题
- 数据报表实时分析:出身对命运的影响到底有多大?