#19003: Python 方法


20060705sean (超簡單的好不好)

School : No School
ID : 77753
IP address : [36.236.120.26]
Last Login :
2020-08-04 21:42:56
a013. 羅馬數字 -- NPSC 模擬試題 | From: [1.173.108.116] | Post Date : 2019-08-24 22:37

Python 可以使用def,大概如下:

 

Nums = {'I' : '1' , 'V' : '5' , 'X' : '10' , 'L' : '50' , 'C' : '100' , 'D' : '500' , 'M' : '1000'}

 

#羅馬 >> 阿拉伯

def RTA(Input):
Sum = 0
if(Input.isupper() == True):
for i in range (0 , len(Input)):
Sum += eval(Nums[Input[i]])
if(i != len(Input) - 1 and (Input[i] == 'I' and (Input[i + 1] == 'V' or Input[i + 1] == 'X'))):Sum -= 2
elif(i != len(Input) - 1 and (Input[i] == 'X' and (Input[i + 1] == 'L' or Input[i + 1] == 'C'))):Sum -= 20
elif(i != len(Input) - 1 and (Input[i] == 'C' and (Input[i + 1] == 'D' or Input[i + 1] == 'M'))):Sum -= 200
else:Sum = ''
return Sum

 

 

#阿拉伯 >> 羅馬

 

def ATR(Input):
Input , Str = str(Input) , ''
for i in range(0 , len(Input)):
Now_num = int(Input[i]) * (10 ** (len(Input) - 1 - i))
if(str(Now_num)[0] != '9' and str(Now_num)[0] != '4'):
i = 6
while(Now_num != 0):
if(Now_num - eval(list(Nums.values())[i]) >= 0):Str , Now_num = Str + list(Nums.keys())[i] , Now_num - eval(list(Nums.values())[i])
else:i -= 1
elif(Now_num == 4000):Str += 'MMMM'
elif(Now_num == 900):Str += 'CM'
elif(Now_num == 400):Str += 'CD'
elif(Now_num == 90):Str += 'XC'
elif(Now_num == 40):Str += 'XL'
elif(Now_num == 9):Str += 'IX'
elif(Now_num == 4):Str += 'IV'
return Str

 

自行縮排,主程式自行編寫。

 

 
ZeroJudge Forum