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

Roman Numeral Counting Method:

Basic characters I V X L C D M
The corresponding Arabic numerals are expressed as 1 5 10 50 100 500 1000
1, the same number ligatures, the number is equal to the number of the sum of these numbers, such as: ⅲ= 3, 2, the small number on the right side of the large number, the number represented is equal to the number of these numbers, such as: ⅷ= 8;ⅻ= 12;3, small numbers, (limited to Ⅰ, X and C) on the left of the large numbers The number represented is equal to the number of the number of large number reduction, such as: ⅳ= 4;ⅸ= 9;4, normal use, the number of ligatures can not be repeated more than three times. (Exception of four O'Clock "IIII" on the Dial) 5. Draw a horizontal line over a number to indicate that the number is enlarged by 1000 times times. Rules: 1, the basic number of Ⅰ, X, C of any one, the number of their own use, or placed on the right of the large number of the number of the composition, not more than three; on the left side of the large number can only use one. 2, can not be the basic number of V, L, D any one as a decimal place on the left of the large number of the method of subtracting the number of methods; 3, V and X left small numbers can only be used Ⅰ. The small numbers on the left of 4, L and C can only be used with X. The small numbers on the left of 5, D and M can only be used in C. (from Baidu Encyclopedia: http://baike.baidu.com/link?url=44M1P1d9WnPI7fucoCuM4FPX5d5ZJogqNHnxxtVTtbgxcqGjKWR8i9bBYakM3DH5ug1wW7 _YLJ0J-5FOVGK0EA)

#include <string.h> #include <stdio.h>int ctoi (char c) {    switch (c) {case        ' I ': return 1;        Case ' V ': return 5;        Case ' X ': return ten;        Case ' L ': return;        Case ' C ': return;        Case ' D ': return;        Case ' M ': return;    }} int Romantoint (char *s) {    int data=0;    int i,j;    for (i=0;s[i+1]!= '; i++) {        if (Ctoi (S[i]) >=ctoi (s[i+1])) Data=data+ctoi (S[i]);        else Data=data-ctoi (S[i]);    }    if (s[i+1]== ') Data=data+ctoi (S[i]);    return data;} void Main () {    printf ("%d\n", Romantoint ("XCIX"));}


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.