13_roman to Integer

Source: Internet
Author: User

Given a Roman numeral, convert it to an integer.

Input is guaranteed to being within the range from 1 to 3999.

Conversion of Roman numerals to Arabic numerals

I 1

X 10

C 100

M 1000

V 5

L 50

D 500

Small numbers in front of large numbers, minus small numbers for large numbers, and small numbers for large numbers after large numbers

 Public classSolution { Public intRomantoint (strings) {intresult =0; Char[] Chlist =S.tochararray (); Dictionary<Char,int> romadic =Newdictionary<Char,int>(); Romadic.add ('I',1); Romadic.add ('X',Ten); Romadic.add ('C', -); Romadic.add ('M', +); Romadic.add ('V',5); Romadic.add ('L', -); Romadic.add ('D', -); intFormer, latter =0;  for(inti =0; I < chlist.length-1; i++) {Former=0; Latter=0; Romadic.trygetvalue (Chlist[i], outformer); Romadic.trygetvalue (Chlist[i+1], outlatter); if(Former <latter) {Result-=former; }                Else{result+=former; }            }                        intLast =0; Romadic.trygetvalue (Chlist.last (), outLast ); Result+=Last ; returnresult; }}

13_roman to Integer

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.