HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)

简介: 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);
        }
    }
}

目录
相关文章
|
9月前
|
算法
【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
26 0
|
2月前
|
存储
leetcode:504. 七进制数
leetcode:504. 七进制数
23 0
|
8月前
剑指offer JZ49把字符串转换成整数
剑指offer JZ49把字符串转换成整数
29 0
|
文件存储
【每日一题Day51】LC1780判断一个数字是否可以表示成三的幂之和 | 进制转换
思路:当我们把二进制转换为十进制时,采用的方法是二的幂之和。那么,本题可以将十进制转换为三进制,当所有位的值为0或者1时,满足题意,返回true;但有一位值为2时,返回false
55 0
P5704 【深基2.例6】字母转换
P5704 【深基2.例6】字母转换
74 0
|
算法
LeetCode-13. 罗马数字转整数(day3)
LeetCode-13. 罗马数字转整数(day3)
95 0
|
数据安全/隐私保护
HDU-2100,Lovekey(大数加法,26进制)
HDU-2100,Lovekey(大数加法,26进制)
12. 整数转罗马数字(LeetCode)
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。
177 0
Leetcode打卡 | No.012 整数转罗马数字
欢迎和小詹一起定期刷leetcode,每周一和周五更新一题,每一题都吃透,欢迎一题多解,寻找最优解!这个记录帖哪怕只有一个读者,小詹也会坚持刷下去的!
80 0
Leetcode打卡 | No.012 整数转罗马数字
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
99 0