a={"M":1000,"D":500,"C":100,"L":50,"X":10,"V":5,"I":1}
b={1000:"M",900:"CM",500:"D",400:"CD",100:"C",90:"XC",50:"L",40:"XL",10:"X",9:"IX",5:"V",4:"IV",1:"I"}
y=list(b.keys())
y=sorted(y,reverse=True)
tf=True
while tf:
n1=input("").rstrip()
if(n1=="#"):break
n=n1.split(" ")
x=[0,0]
for i in range(2):
for j in range(len(n[i])):
if(j==len(n[i])-1):
x[i]+=a[n[i][j]]
elif(a[n[i][j]]>a[n[i][j+1]]):
x[i]+=a[n[i][j]]
elif(a[n[i][j]]<a[n[i][j+1]]):
x[i]-=a[n[i][j]]
else:
x[i]+=a[n[i][j]]
z=x[0]-x[1]
if(z==0):
print("ZERO")
else:
s=""
while (z!=0):
for i in range(len(y)):
if (z>=y[i]):
z-=y[i]
s+=b[y[i]]
break
print(s)
a={"M":1000,"D":500,"C":100,"L":50,"X":10,"V":5,"I":1}
b={1000:"M",900:"CM",500:"D",400:"CD",100:"C",90:"XC",50:"L",40:"XL",10:"X",9:"IX",5:"V",4:"IV",1:"I"}
y=list(b.keys())
y=sorted(y,reverse=True)
tf=True
while tf:
n1=input("").rstrip()
if(n1=="#"):break
n=n1.split(" ")
x=[0,0]
for i in range(2):
for j in range(len(n[i])):
if(j==len(n[i])-1):
x[i]+=a[n[i][j]]
elif(a[n[i][j]]>a[n[i][j+1]]):
x[i]+=a[n[i][j]]
elif(a[n[i][j]]<a[n[i][j+1]]):
x[i]-=a[n[i][j]]
else:
x[i]+=a[n[i][j]]
z=x[0]-x[1]
if(z==0):
print("ZERO")
else:
s=""
while (z!=0):
for i in range(len(y)):
if (z>=y[i]):
z-=y[i]
s+=b[y[i]]
break
print(s)
程式碼全部註解掉
只留
import sys
while True:
n1=sys.stdin.readline()[:-2]
if(n1=="#"):break
print(n1)
也會TLE,為什麼??
ZJ 在「測試執行」的換行字元是 '\r\n' 沒錯,
但是「送出執行」測資的換行字元只是 '\n' 而已, (除非出題者故意出'\r\n')
基本上只要在 Python 的 Shell 上能正確的輸入輸出在「送出解答」就會是對的了~
另外,
如果真的要能同時處理兩者的狀況可以試著 sys.stdin.readline().strip() ,
strip() 會清除頭尾所有空白字元(常見包括 ' ' , '\n' , '\r' , '\t' ),
也就是無論是 'I I\n' 或 'I I\r\n' 都會變成 'I I' ~
以上希望有幫助到你~ OwO