Convert a Roman number to the corresponding number of Arabs

Source: Internet
Author: User

Arabic number, as the name implies, we usually use the most number, for example, 1,2,3,4, ...

The number of Roman numerals is the earliest representation of numbers. The basic characters are:I, V, X, L, C, D, M, corresponding numbers

respectively: 1,5,10,50,100,500,1000.

The following gives a count of Roman numbers:

1. Two identical characters are written next to each other, indicating the addition. For example: XX into the Arab number is 20.

2. If a character represents a number larger than the character on the left, then converted to Arabic, that's the number minus

The number on the left.

For example, the number of Arabs represented by IX is 10-1 = 9.

The number of Arabs represented by ICMI is: 1000-(IC) +i = 1000-(100-1) +1 = 902.

You must first find the largest character in the Roman number given in the conversion.

Below is the code:

#include <stdio.h> #include <string.h> #include <assert.h> #define N 7char Digit[n] = {' I ', ' V ', ' X ', ' L ', ' C ', ' D ', ' M '};int values[n] = {1, 5, ten, 0, 100,500,1000};int digittovalue (char ch)//character converted to corresponding Arabic number {int i = 0;for (i =; i < N; i++) {if (digit[i] = = ch) return values[i];} return 0;} int Findmaxindex (char str[], int L, int R)//The subscript for the maximum character {assert (L >= 0 &&r >= 0); int i = 0;int max = Digittovalu E (str[l]); int maxindex = l;for (i = l+1;i <= r;i++) {int tmp = Digittovalue (Str[i]);//convert letters to corresponding numbers if (tmp > Max) {max = Tmp;maxindex = i;}} return maxindex;} int Romentonumber (char str[],int l,int R)//conversion function {if (L = = R) {return digittovalue (Str[l]);} else if (L > R) {return 0;} Else{int maxindex = Findmaxindex (str, L, R); int left = Romentonumber (str, L, maxIndex-1); int right = Romentonumber (str,m Axindex + 1, R); int num = Digittovalue (Str[maxindex]); return num-left + right;}} int main () {char str[] = "Imcci"; int r = Romentonumber (str,0,4);p rintf ("%d", r); system ("pause"); return 0;} 


Implementation is done in part by recursion. Find the maximum character of the number and then use the recursive method to get the left

Number, and then the number to the right. It is convenient to define several digital characters of Roman numerals as global variables.

Digittovalue function Lookup.



Convert a Roman number to the corresponding number of Arabs

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.