# PHP (4) "Roman numeral to Integer" algorithm problem

Source: Internet
Author: User

Label:length   split   unc   i+1   pre     Rules    str    one     applies to

The original title is this: the Roman numerals contain the following seven characters: I, V, X, L,c,d and M. i=1,v=5,x=10,l=50,c=100,d=500,m=1000 For example, the Roman numeral 2 writes II, which is two parallel 1. 12 write XII, i.e. X + II. 27 Write the XXVII, that is XX + V + II. Usually, the number of Roman numerals is small and the numbers are on the right side of the large numbers. But there are special cases, such as 4 do not write IIII, but IV. The number 1 is on the left side of the number 5, the number represented is equal to the large number 5 decreases the number 1 gets the value 4. Similarly, the number 9 is represented as IX. This special rule only applies to the following six cases:
• I can be placed on the left of V (5) and X (10) to represent 4 and 9.
• X can be placed on the left of L (50) and C (100) to represent 40 and 90.
• C can be placed on the left of D (500) and M (1000) to represent 400 and 900. The
• converts it to an integer given a Roman number. The input is guaranteed to be within the range of 1 to 3999.
• Example 1:

Input: "III"
Output: 3

• li>

Example 2:

Input: "IV"
Output: 4

• Example 1:

Output In: "MCMXCIV"
Output: 1994

``Class test{Public Function A5 (\$str) {\$num = 0;            \$num _arr = Str_split (\$STR);            \$length = count (\$num _arr);                    for (\$i =0; \$i < \$length; \$i + +) {switch (\$num _arr[\$i]) {                                Case ' I ': switch (\$num _arr[\$i +1]) {                                    Case ' V ': \$num + = 4;                                    \$i + +;                                Break                                    Case ' X ': \$num + = 9;                                    \$i + +;                                Break                                    Default: \$num + = 1;                            Break                    } break;                        Case ' V ': \$num + = 5;         Break           Case ' X ': switch (\$num _arr[\$i +1]) {                                    Case ' L ': \$num + = 40;                                    \$i + +;                                Break                                    Case ' C ': \$num + = 90;                                    \$i + +;                                Break                                    Default: \$num + = 10;                            Break                    } break;                        Case ' L ': \$num + = 50;                    Break                                Case ' C ': switch (\$num _arr[\$i +1]) {                                    Case ' D ': \$num + = 400;                                   \$i + +; Break                                    Case ' M ': \$num + = 900;                                    \$i + +;                                Break                                    Default: \$num + = 100;                            Break                    } break;                        Case ' D ': \$num + = 500;                    Break                        Case ' M ': \$num + = 1000;                    Break                Default:break;        }} echo \$num; }}``

PHP (4) "Roman numeral to Integer" algorithm problem

Related Keywords:
Related Article