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:

  1. 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.

  2. 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.
  3. 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.
  4. Only the I, X, or C can be used as subtractive numerals.
  5. 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);
        }
    }
}
时间: 2024-10-14 13:04:25

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

javascript将10进制数转换为2进制

javascript中将10进制数转换为2进制有两种方式 一种是直接用toString(2)这个方法,一种是自己写一个方法换算,以下是代码: var num = 11; var str = num.toString(2); /*将十进制数转变为二进制数*/ function dec2bin(num){ var result = ""; if(num == 0){ return "0"; } while(num > 0){ result = num % 2 +

c++描述将一个2进制数转化成10进制数(用到初始化栈,进栈,入栈)

1 /* 2 c++描述将2进制数转化成10进制数 3 问题,1.初始化栈后,用new,不知道delete是否要再写一个函数释放内存, 4 还是在哪里可以加上delete 5 2.如果栈满了,我要分配多点空间,我想的办法是先用delete删除之前申请的 6 空间,再用new重新申请,但是c语言有一个函数 7 s->base =(ElemType*) realloc(s->base,(s->stackSize + STACKINCREMENT) * sizeof(ElemType));//

[转]as3 算法实例【输出1 到最大的N 位数 题目:输入数字n,按顺序输出从1 最大的n 位10 进制数。比如输入3,则输出1、2、3 一直到最大的3 位数即999。】

思路:如果我们在数字前面补0的话,就会发现n位所有10进制数其实就是n个从0到9的全排列.也就是说,我们把数字的每一位都从0到9排列一遍,就得到了所有的10进制数. 1 /** 2 *ch 存放数字 3 *n n位数 4 *index 计数值 5 **/ 6 private function num(ch:Array,n:int,index:int):void 7 { 8 if(index==n) 9 { 10 trace(ch); 11 return; 12 } 13 for(var i:in

10进制数转为16进制

问题 输入一个10进制数,输出这个10进制数对应的16进制数 思路 首先用10进制数除以16,余数则为不能进位的数字,则写在最低位上,商的含义是有多少个16,如果商大于等于16的话,意味还可以接着进位,那么用商接着除以16,余数写在倒数第二位上--以此进行下去,直到不能进位为止 代码 #include <iostream> #include<string> using namespace std; string m = "0123456789ABCDEF"; i

汇编:2进制数转为10进制数

1 ;功能:16位的2进制数转为5位的10进制数并输出 2 DATAS SEGMENT 3 Num dw 1111100111B ;带转换的二进制数(<=16位 测试用例对应的10进制为999) 4 buffer db 5 dup(0) ;用来保存结果 5 jm dw 10000,1000,100,10,1 ;用于每次循环的除数 6 DATAS ends 7 CODES SEGMENT 8 ASSUME CS:CODES, DS:DATAS 9 START: 10 mov AX,DATAS 11

c语言将2进制数转化为10进制数(栈的初始化,进栈,出栈)

1 //c语言描述 将2进制转化为10进制 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #define STACK_INIT_SIZE 20 6 #define //栈满后再分配 7 8 typedef char ElemType; 9 typedef struct 10 { 11 ElemType *base; //栈底 12 ElemType *top; //栈底 13 int s

32位16进制浮点数转化成10进制数

{ BYTE s[4]; s[3]=0x42; s[2]=0x1C; s[1]=0x80; s[0]=0x00; float *pf=(float*)s; printf("10进制%g\n",*pf); }

从文件读入16进制数转化为10进制数再输出到文件中

sSN LMDscandata 1 1 B98C27 0 0 85C0 85C3 F55D73C5 F55DCC81 0 0 7 0 0 1388 168 0 1 DIST1 3F800000 00000000 DBBA0 1388 B5 136C 1373 136B 1389 1398 1356 136D 1386 137B 139C 13C4 13F7 1531 174D 1751 1755 1765 176C 1777 177B 1784 1791 1796 17A8 17C0 17C6

ORACLE 36进制和10进制,互相转换函数

第一部分 --36转10进制 create or replace function f_36to10 (str varchar) return int  is returnValue int;   str36     varchar(36);   subWork   varchar(1);   workIndex   int;   len     int;   i       int; begin returnValue:= 0;   str36 := '123456789ABCDEFGHIJK