Basic program to convert integer to Roman numerals?

后端 未结 24 1376
孤独总比滥情好
孤独总比滥情好 2020-11-30 11:52

I\'m trying to write a code that converts a user-inputted integer into its Roman numeral equivalent. What I have so far is:

The point of the generate_

24条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 12:11

    Write a simple code which takes the number, decreases the number, and appends its corresponding value in Roman numerals in a list and finally converts it in the string. This should work for range 1 to 3999

    for no in range(1,3999):
        r_st=list()
        a=no
        while(no>1000):
            r_st.append("M")
            no-=1000
        if(no>899 and no<1000):
            r_st.append("CM")
            no-=900
        if(no>499):
            r_st.append("D")
            no-=500
        if(no>399):
            r_st.append("CD")
            no-=400
        while(no>99):
            r_st.append("C")
            no-=100
        if(no>89):
            r_st.append("LC")
            no-=90
        if(no>50):
            r_st.append("L")
            no-=50
        if(no>39):
            r_st.append("XL")
            no-=40
        while(no>9):
            r_st.append("X")
            no-=10
        if(no>8):
            r_st.append("IX")
            no-=9
        if(no>4):
            r_st.append("V")
            no-=5
        if(no>3):
            r_st.append("IV")
            no-=4
        while(no>0):
            r_st.append("I")
            no-=1
        r_st=''.join(r_st)
        print(a,"==",r_st)
    

提交回复
热议问题