Roman Numeral decimal number __java

Source: Internet
Author: User

Topic Requirements:



The ancient Roman Empire created a glorious human civilization, but their digital notation is indeed a bit cumbersome, especially when it comes to large numbers, which now seem unbearable, so it is rarely used in modern times. This is not because of the intellectual problems of the inventor of the law, but because of a religious reason, the religion of that time banned the concept of 0 in numbers.
The representation of Roman numerals relies mainly on the following basic symbols:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Here, we only introduce the representation of numbers within 1000.
How many times a single symbol repeats. Repeat up to 3 times. For example: CCC said 20, but 150 does not use LLL, this rule applies only to I X C M.
If the adjacent level of large units in the right, small units on the left, indicating large units in the deduction of small units. For example, IX represents 9 IV for 4 XL for 40 more examples see the following table, have you found the pattern?
i,1
ii,2
iii,3
iv,4
v,5
vi,6
vii,7
viii,8
ix,9
x,10
xi,11
xii,12
xiii,13
xiv,14
xv,15
xvi,16
xvii,17
xviii,18
xix,19
xx,20
xxi,21
xxii,22
xxix,29
xxx,30
Xxxiv,34
Xxxv,35
xxxix,39
xl,40
l,50
li,51
lv,55
lx,60
lxv,65
lxxx,80
xc,90
xciii,93
xcv,95
xcviii,98
xcix,99
c,100
cc,200
ccc,300
cd,400
d,500
dc,600
dcc,700
dccc,800
cm,900
cmxcix,999
The request of this topic is: Please write the program, the user input several Roman numeral string, the program output corresponding decimal representation.
The input format is: The first line is integer n, which means that there are n Roman digits (n<100) next. After each row a Roman numeral. The Roman numeral size is no more than 999.
Requires the program to output n rows, which is the decimal data corresponding to Roman numerals.
For example, user input:
3
LXXX
Xciii
Dccii
The program should output:
80
93
702
Attention:
Please carefully debug. Your program only gets a chance to score when it can run the right results.
The input data used when marking the papers may be different from the instance data given in the test paper.


Input Code:


Import Java.util.Scanner;
     public class Main {public static void main (string[] args) {Scanner in=new Scanner (system.in);
     int N=in.nextint ();
     In.nextline ();
     string [] str= new String [n];
      for (int i=0;i<n;i++) {str[i]=in.nextline ();
      System.out.println (Zhuanhuan (str[i));
     i++;
     The public static int Luoma (char c) {switch (c) {case ' I ': return 1;
     Case ' V ': return 5;
     Case ' X ': return 10;
     Case ' L ': return 50;
     Case ' C ': return 100;
     Case ' D ': return 500;
     Case ' M ': return 1000;
   return 0;
	  public static int Zhuanhuan (String str) {int sum=0;
	 int a,b;
	   for (int i=0;i<str.length (); i++) {A=luoma (Str.charat (i));
	   if (I+1<str.length ()) {B=luoma (Str.charat (i+1));
	   else {b=0;
		   } if (B>a) {sum+= (b-a);
	   i++;
	   else {sum+=a;
   return sum; }
}










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.