HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)
Problem Description
The Romans used letters from their Latin alphabet to represent each of the seven numerals in their number system. The list below shows which
letters they used and what numeric value each of those letters represents:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
Using these seven numerals, any desired number can be formed by following the two basic additive and subtractive rules. To form a number using
the additive rule the Roman numerals are simply written from left to right in descending order, and the value of each roman numeral is added
together. For example, the number MMCLVII has the value 1000 + 1000 + 100 + 50 + 5 + 1 + 1 = 2157. Using the addition rule alone could lead to
very long strings of letters, so the subtraction rule was invented as a result. Using this rule, a smaller Roman numeral to the left of a larger one is
subtracted from the total. In other words, the number MCMXIV is interpreted as 1000 - 100 + 1000 + 10 - 1 + 5 = 1914.
Over time the Roman number writing system became more standardized and several additional rules were developed. The additional rules used today
are:
- The I, X, or C Roman numerals may only be repeated up to three times in succession. In other words, the number 4 must be represented as IV
and not as IIII. - The V, L, or D numerals may never be repeated in succession, and the M numeral may be repeated as many 2. times as necessary.
- Only one smaller numeral can be placed to the left of another. For example, the number 18 is represented as XVIII but not as XIIX.
- Only the I, X, or C can be used as subtractive numerals.
- A subtractive I can only be used to the left of a V or X. Likewise a X can only appear to the left of a L or C, and a C can only be used to the
left of a D or M. For example, 49 must be written as XLIX and not as IL.
Your goal is to write a program which converts Roman numbers to base 10 integers.
Input
The input to this problem will consist of the following:
A line with a single integer “N” (1 ≤ N ≤ 1000), where N indicates how many Roman numbers are to be converted.
A series of N lines of input with each line containing one Roman number. Each Roman number will be in the range of 1 to 10,000 (inclusive)
and will obey all of the rules laid out in the problem’s introduction.
Output
For each of the N Roman numbers, print the equivalent base 10 integer, one per line.
Sample Input
3
IX
MMDCII
DXII
Sample Output
9
2602
512
罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。
1、重复数次:一个罗马数字重复几次,就表示这个数的几倍。
2、右加左减:
2.1 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
2.2 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
2.3 左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
2.4 但是,左减时不可跨越一个位数。比如,99不可以用IC(100 - 1)表示,而是用XCIX([100 - 10] + [10 - 1])表示。(等同于阿拉伯数字每位数字分别表示。)
2.5 左减数字必须为一位,比如8写成VIII,而非IIX。
注意的就是:I只能在V,X的左边。X只能在L,C的左边。C只能在D,M的左边。
知道这些就可以AC了。
import java.util.Scanner;
/**
* @author 陈浩翔
* 2016-6-5
*/
public class Main{
static char[] chS={'I','V','X','L','C','D','M'};
static int[] chN={1,5,10,50,100,500,1000};
static String[] strS={"IV","IX","XL","XC","CD","CM"};
static int[] strN={2,2,20,20,200,200};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
String str=sc.next();
int num=0;
for(int i=0;i<str.length();i++){
for(int j=0;j<chS.length;j++){
if(str.charAt(i)==chS[j]){
num+=chN[j];
break;
}
}
}
String s="";
for(int i=1;i<str.length();i++){
s=""+str.charAt(i-1)+str.charAt(i);
for(int j=0;j<strS.length;j++){
if(s.equals(strS[j])){
num-=strN[j];
break;
}
}
}
System.out.println(num);
}
}
}
HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)的更多相关文章
- HDOJ(HDU) 1877 又一版 A+B(进制、、)
Problem Description 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数. Input 输入格式:测试输 ...
- [转]as3 算法实例【输出1 到最大的N 位数 题目:输入数字n,按顺序输出从1 最大的n 位10 进制数。比如输入3,则输出1、2、3 一直到最大的3 位数即999。】
思路:如果我们在数字前面补0的话,就会发现n位所有10进制数其实就是n个从0到9的全排列.也就是说,我们把数字的每一位都从0到9排列一遍,就得到了所有的10进制数. /** *ch 存放数字 *n n ...
- c++描述将一个2进制数转化成10进制数(用到初始化栈,进栈,入栈)
/* c++描述将2进制数转化成10进制数 问题,1.初始化栈后,用new,不知道delete是否要再写一个函数释放内存, 还是在哪里可以加上delete 2.如果栈满了,我要分配多点空间,我想的办法 ...
- c语言将2进制数转化为10进制数(栈的初始化,进栈,出栈)
//c语言描述 将2进制转化为10进制 #include <stdio.h> #include <stdlib.h> #include <math.h> #defi ...
- python中的2、8、16、10进制之间的转换
python除法的坑 众所周知,python除法有两个运算符,一个是/,还有一个是//,那么这两个有什么不同之处呢? 从图片可以得知,使用//返回一个float类型,而使用/返回一个int类型.我们总 ...
- ORACLE 36进制和10进制,互相转换函数
第一部分 --36转10进制 create or replace function f_36to10 (str varchar) return int is returnValue int; s ...
- HDU 2352 Verdis Quo
罗马数字转化为十进制的值 题目非常的长 提取有效信息 并且介绍很多规则 但是事实上有用的信息就是如何加 什么时候减 当当前字母小于下一个字母时 减去当前字母的值 #include <iostre ...
- 【LeetCode】将罗马数字转换成10进制数
Roman to Integer Given a roman numeral, convert it to an integer. 首先介绍罗马数字 罗马数字共有七个,即I(1),V(5),X(10) ...
- 已知从BUF开始存放了10个字类型有符号数据,编程求出这10个数中的最大数和最小数(将最大数存入MAX字单元、最小数存入MIN字单元),并将其以10进制数的形式在屏幕上显示出来。
data segment pmax db 0dh,0ah , 'MAX : ','$' pmin db 0dh,0ah , 'MIN : ','$' buf ...
随机推荐
- Android中Matrix的pre post set方法理解(转载来源:Linux社区 作者:zjmdp)
虽说以前学习过线性代数和图形学原理,但是在实际中碰到matrix还是疑惑了好一阵子,今天通过向同事请教终于找到一点门路,特总结如下: Matrix主要用于对平面进行缩放,平移,旋转以及倾斜操作,为简化 ...
- IQueryable接口与IEnumberable区别
IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,take 这些 ...
- 认识linux权限
首先,我们来了解下linux系统的用户和用户组 场景:公司里有两个项目组:小组A和小组B:A.B.C是小组A的成员,甲.乙是小组B的成员.为了保密起见,小组内的进度.文档.程序都有小组内公开.比如小组 ...
- Dom操作--跑马灯效果
这里给园友们演示的是Dom操作实现跑马灯效果,相信我们很多人都用Winform实现过跑马灯效果,其中的关键就是Tirm控件,那么在Dom操作中是用setInterval方法来实现隔一段时间执行一段代码 ...
- JDK重要包和Java学习方法论
以下内容摘自:万能的林萧说:一篇文章教会你,如何做到简历中要求的“要有扎实的Java基础” 第一级别:精读源码 该级别包含的包如下: java.io java.lang java.util 第二 ...
- PHP - PHPExcel操作xls文件
读取中文的xls.csv文件会有问题,网上找了下资料,发现PHPExcel类库好用,官网地址:http://phpexcel.codeplex.com/ 1.读取xls文件内容 <?php // ...
- word 2013 没有控件菜单怎么办,添加控件菜单
方法/步骤 打开word软件,然后点击菜单栏中最左边的“文件”菜单项,如下图红色方框所示 2 点击文件后,就打开word的设置对话框,然后在左边的设置列表中点击“自定义功能区”,打开自定义功能区设 ...
- PHP源码阅读笔记一(explode和implode函数分析)
PHP源码阅读笔记一一.explode和implode函数array explode ( string separator, string string [, int limit] )此函数返回由字符 ...
- JS简单仿QQ聊天工具的制作
刚接触JS,对其充满了好奇,利用刚学到的一点知识,写了一个简单的仿QQ聊天的东西,其中还有很多的不足之处,有待慢慢提高. 功能:1.在输入框中输入内容,点击发送,即可在上方显示所输入内容. 2.点击‘ ...
- Dynamips做CCNA的实验,说是找不到telnet的解决方案
01.如果你的系统是32位的系统. 控制面板-程序与功能-启动或关闭windows功能-开启telnet(重启计算机就可以用telnet了) 02.如果你的系统是64位的系统. (1)控制面板-程序与 ...