Last active
July 29, 2016 06:32
-
-
Save LotusChing/5fe312bc9eed133623741adea1dbc28a to your computer and use it in GitHub Desktop.
change integer number to roman numeral
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dict = { | |
'1' : 'I', | |
'5' : 'V', | |
'10' : 'X', | |
'50' : 'L', | |
'100' : 'C', | |
'500' : 'D', | |
'1000' : 'M', | |
} | |
def g(n): | |
if n == 0: pass | |
if n == 5: return dict['5'] | |
if n == 9: return dict['1'] + dict['10'] | |
if n < 4: | |
result = '' | |
for i in range(n): | |
result += dict['1'] | |
return result | |
if n == 4: return dict['1'] + dict['5'] | |
if n < 9: | |
result = 'V' | |
n = n - 5 | |
for i in range(n): | |
result += dict['1'] | |
return result | |
def s(n): | |
if n == 0: pass | |
if n == 1: return dict['10'] | |
if n == 5: return dict['50'] | |
if n == 4: return dict['10'] + dict['50'] | |
if n == 9: return dict['10'] + dict['100'] | |
if n < 5: | |
result = '' | |
for i in range(n): | |
result += dict['10'] | |
return result | |
if n < 10: | |
result = 'L' | |
n = n - 5 | |
for i in range(n): | |
result += dict['10'] | |
return result | |
def b(n): | |
if n == 0: pass | |
if n == 1: return dict['100'] | |
if n == 5: return dict['500'] | |
if n == 4: return dict['100'] + dict['500'] | |
if n == 9: return dict['100'] + dict['1000'] | |
if n < 5: | |
result = '' | |
for i in range(n): | |
result += dict['100'] | |
return result | |
if n < 10: | |
result = 'L' | |
n = n - 5 | |
for i in range(n): | |
result += dict['100'] | |
return result | |
def q(n): | |
if n > 3: return 'Out of Range...' | |
if n == 0: pass | |
if n == 1: return dict['1000'] | |
if n < 4: | |
result = '' | |
for i in range(n): | |
result += dict['1000'] | |
return result | |
if n < 10: | |
result = 'L' | |
n = n - 5 | |
for i in range(n): | |
result += dict['1000'] | |
return result | |
if __name__ == '__main__': | |
number = input('Please input Integer to Roman numerals: ') | |
if len(number) == 1: g(int(number)) | |
if len(number) == 2: | |
sn, gn = number | |
print(s(int(sn)) + g(int(gn))) | |
if len(number) == 3: | |
bn, sn, gn = number | |
print(b(int(bn)) + s(int(sn)) + g(int(gn))) | |
if len(number) == 4: | |
qn, bn, sn, gn = number | |
print(q(int(qn)) + b(int(bn)) + s(int(sn)) + g(int(gn))) | |
''' | |
简直屈辱,我好笨啊~ 居然写了接近快100行!!! 丢死人啦!!!! 去看大牛们的思路,争取用最简洁的方式写出来! | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment