Converting integers to roman numerals

前端 未结 29 2456
走了就别回头了
走了就别回头了 2020-12-02 09:16

I\'m trying to write a function that converts numbers to roman numerals. This is my code so far; however, it only works with numbers that are less than 400. Is there a quick

29条回答
  •  萌比男神i
    2020-12-02 09:33

    I find BrunoLM's code very simple and elegant but the From(...) function really needs to check if the source is a valid roman numeral.
    Something like this

    public static bool IsValidRomanNumber(string source) {
            bool result = true;
    
            string[] invalidCouples = { "VV", "LL", "DD", "VX", "VC", "VM", "LC", "LM", "DM", "IC", "IM", "XM" };
    
            foreach (string s in invalidCouples) {
                if (source.Contains(s)) {
                    result = false;
                    break;
                }
            }
    
            return result;
        }
    

提交回复
热议问题