博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Algorithms—13.Roman to Integer
阅读量:2457 次
发布时间:2019-05-11

本文共 1333 字,大约阅读时间需要 4 分钟。

package leetcode;public class RomantoInt {public static void main(String[] args) {String s="MCDLXXVI";int a;int b;int c;int d;String sa="";String sb="";String sc="";String sd="";String flag="千";for (int i = 0; i < s.length(); i++) {String t=s.substring(i,i+1);if (flag.equals("千")) {if(t.equals("M")){sa+=t;continue;}else {flag="百";}}if (flag.equals("百")) {if (t.equals("C")||t.equals("D")||t.equals("M")) {sb+=t;continue;}else {flag="十";}}if (flag.equals("十")) {if (t.equals("X")||t.equals("L")||t.equals("C")) {sc+=t;continue;}else {flag="个";}}if (flag.equals("个")) {if (t.equals("I")||t.equals("V")||t.equals("X")) {sd+=t;continue;}}}System.out.println("sa="+sa);System.out.println("sb="+sb);System.out.println("sc="+sc);System.out.println("sd="+sd);a=sa.length();if (sb.length()==0) {b=0;}else if (sb.contains("M")) {b=9;}else if(sb.substring(0,1).equals("D")){b=5+sb.length()-1;}else if (sb.contains("D")) {b=4;}else {b=sb.length();}if (sc.length()==0) {c=0;}else if (sc.contains("C")) {c=9;}else if(sc.substring(0,1).equals("L")){c=5+sc.length()-1;}else if (sc.contains("L")) {c=4;}else {c=sc.length();}if (sd.length()==0) {d=0;}else if (sd.contains("X")) {d=9;}else if(sd.substring(0,1).equals("V")){d=5+sd.length()-1;}else if (sd.contains("V")) {d=4;}else {d=sd.length();}System.out.println("该罗马数字="+(1000*a+100*b+10*c+d));}}

转载地址:http://vkshb.baihongyu.com/

你可能感兴趣的文章
唱吧录制的歌曲转换成mp3_录制开放文化歌曲
查看>>
Mercy Health为其主要门户网站设置了Drupal和Alfresco
查看>>
gpl2 gpl3区别_自由软件基金会将举办有关GPL执法和法律道德的研讨会
查看>>
python 下三角矩阵_Python | 矩阵的上三角
查看>>
Java StringBuffer CharSequence subSequence(int spos,int epos)方法与示例
查看>>
Java Collections unmodifiableList()方法与示例
查看>>
python 示例_Python日历类| itermonthdates()方法与示例
查看>>
物联网互联收费_联网| 互联网络能力问答 套装1
查看>>
kotlin中判断字符串_Kotlin程序计算字符串中每个字符的出现
查看>>
math asin_Java Math类静态double asin(double d)方法及示例
查看>>
如何从JavaScript中的数组替换元素?
查看>>
程序如何检查堆栈溢出_通过使用堆栈检查平衡的括号(C ++程序)
查看>>
rip1 rip2_RIP的完整形式是什么?
查看>>
ruby 集合 分组_在Ruby中找到集合的长度
查看>>
sizeof函数_PHP sizeof()函数与示例
查看>>
python函数示例_abs()函数以及Python中的示例
查看>>
计算机图形学 顶点定义_在计算机图形学中定义一个圆
查看>>
斯威夫特山地车_斯威夫特| 打印“ Hello World”程序
查看>>
sql中len函数_在SQL中使用LEN(),ROUND()函数
查看>>
python二分法查找程序_Python程序查找标准偏差
查看>>