import java.util.*;
public class Solution 
{
    public static void main (String []args)
    {
        decimalToRoman(20);
    }
    
    //NUMERALS numeral = NUMERAL.1000.value();

    static int remainingNumber;
    
    static int counter=1;

    //this will be remainder once it passes through the switch statement


    public static String decimalToRoman(int num) 
    
    {
        String conversion="";
        int currentNumeral=0;
        int match=0;
        char fourInRowNumeral=' ';
        char incorrectPredecessor=' ';
        String successor="";

        String [] nextHighest = new String[8];
        nextHighest[0] = "I=>V";
        nextHighest[1] = "V=>X";
        nextHighest[2] = "X=>L";
        nextHighest[3] = "L=>C";
        nextHighest[4] = "C=>D";
        nextHighest[5] = "D=>M";


        //M=1000, C=100, D=500, L=50, X=10,  V=5,  I=1



        //these have to be descending order

   

        //return "test";

        //I will first attempt this from recursive perspective..
        //using a class level variable

        //This should much easier than conversion numeral => decimal number

        //we only need to worry about the valid subtractive notations (as a pair)
        
        //do not need to worry about adjacent subtractive notation being in the same range

        //not relevant
        //i.e  IX XC
             //range (1-10)  (range 10-100)

        // these are the valid combinations....

        //String IV =  "IV";
        //String IX =  "IX";
        //String XL =  "XL";
        //String XC =  "XC";
        //String CD=   "CD"; 
        //String CM=   "CM";

        //Also need to consider this rule
        //The xcletters V, L, and D are not repeated.


        //I will work this example using the following two numbers
        //27  and also 19
        //both will bring their own challenges

       
            //main structure of code
            if (num==0)
            {
                return conversion;
            }
                  
            else
            {
                            
            //M=1000, C=100, D=500, L=50, X=10,  V=5,  I=1

            //only way to tackle this challenge is using an if else
            //and check if larger than numbers
            //1000, 500, 100, 50, 10, 5 , 1
            //Map will not work!!!
            //Switch will not work!!!
            //So similar to numeral => decimal, there will be no collections!!!!!

            //do
            //{

                 do
                {

                if (num >= 1000) 
                {
                    currentNumeral++;
                    conversion = conversion + "M";
                    num=num-1000;
                } 
                
                else if (num>=500) 
                {
                    currentNumeral++;
                    conversion = conversion + "D";
                    num=num-500;
                }

                else if (num>=100) 
                {
                    
                    currentNumeral++;
                    conversion = conversion + "C";
                    num=num-100;
                }

                 else if (num>=50) 
                {
                    
                    currentNumeral++;
                    conversion = conversion + "L";
                    num = num - 50;
                }

                 else if (num>=10) 
                {
                    
                    currentNumeral++;
                    conversion = conversion + "X";
                    num = num -10;
                }

                 else if (num>=5) 
                {
                   
                    currentNumeral++;
                    conversion = conversion + "V";
                    num = num - 5;
                }

                else 
                {
                    
                    currentNumeral++;
                    conversion = conversion + "I";
                    num = num - 1;

                }
            //}    while (conversion.length()<=3)
            //this might continue to run indefinitely....
            //so its a big issue having this....

            //it can be tricky having lots of nested do while loops
            //from previous research, if the outer breaks...
            //it will continue to run the inner loop
            //Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.
            //I am ensuring this can occur

            char lastNumeralInConversion;
            int lengthConversion = conversion.length();

            if (currentNumeral==5)
            {
                do
                    {
                        counter++;                       

                        if (conversion.charAt(lengthConversion-counter) == conversion.charAt(lengthConversion-1))
                        {
                            fourInRowNumeral = conversion.charAt(conversion.length()-1);
                            incorrectPredecessor = conversion.charAt(conversion.length()-5);

                            match++;
                            

                        }  //end of big if

                            }while(counter<6);

                        if (match==4)
                        {
                            //{

                        //now need to add next higher numeral
                        //this will be slightly more challenging since we don't have conversion in any Collection at all.
                        
                        //Let's say 
                         //four in a row...
                        //now need to remove the four in a row
                        //also need to remove the one before in conversion...

                        //for instance
                        //94 might appear as

                        //94  
                        //-50    L
                        //-10    X
                        //-10    X
                        //-10    X
                        //-10    X

                        //then need to input  XC
                        //and let the process start again from
                        //94-90

                        //removing 5 characters
                        conversion=conversion.substring(conversion.length()-5);
                        
                        //this will input the X back in
                        conversion = conversion + fourInRowNumeral;

                        //now need to get the next highest value to L

                        for (String g: nextHighest)
                        {
                            if (g.indexOf(String.valueOf(incorrectPredecessor)+"=>")!=-1)

                            //if (g.indexOf(Character.toString(incorrectPredecessor+"=>")))
                            {
                                //we know notation of Stirngs are C=>D

                                successor = g.substring(4);
                                
                            }
                        }

                        conversion = conversion + successor;
                    

            }   //end of if match =4


                        }    //end of if match  (current numeral=5)                         




        }  while (num>0);   
        
        }  //end of else    

        return conversion;  

}
}