import sys
def gcd( m, n):
var = 0
a = m
b = n
c = 0
if(a>b):
while 1 :
if(b != 0):
c= int(a%b)
a = b
b= c
else:
break
return a
elif(a<b):
while 1 :
if(a != 0):
c= int(b%a)
b = a
a= c
else:
break
return b
try:
j = 100
while j >0:
num =input("")
num1,num2 = num.split()
n1 = int(num1)
n2 = int(num2)
j -= 1
print(gcd(n1,n2))
except Exception as e:
pass
import sys
def gcd( m, n):
var = 0
a = m
b = n
c = 0
if(a>b):
while 1 :
if(b != 0):
c= int(a%b)
a = b
b= c
else:
break
return a
elif(a<b):
while 1 :
if(a != 0):
c= int(b%a)
b = a
a= c
else:
break
return b
try:
j = 100
while j >0:
num =input("")
num1,num2 = num.split()
n1 = int(num1)
n2 = int(num2)
j -= 1
print(gcd(n1,n2))
except Exception as e:
pass
不知道為甚麼 會過不了
目前看到的問題主要有2個:
1. 在 gcd() 中你分別處理了 a<b 和 a>b 的狀況,
但卻沒考慮到 a==b 的情況,
所以當 a==b 時你的程式並不會回傳任何東西(Python的函數若沒回傳值相當於回傳 None ),
故會發現輸出的就是"None"~
2. 此題有多筆測資但我們並不知道有幾筆測資,
所以必須持續輸入直到讀到EOF(檔案結尾)才結束,
你是有做到這點沒錯但你卻只讓你的迴圈執行100次,
然而很不幸的本題的測資數量超過100筆,
所以會無法處理到更後面的測資,
其實只要處理好EOF的狀況直接用無限迴圈就OK了~
以上希望有幫助到你~ OwO
import sys
def gcd( m, n):
var = 0
a = m
b = n
c = 0
if(a>b):
while 1 :
if(b != 0):
c= int(a%b)
a = b
b= c
else:
break
return a
elif(a<b):
while 1 :
if(a != 0):
c= int(b%a)
b = a
a= c
else:
break
return b
try:
j = 100
while j >0:
num =input("")
num1,num2 = num.split()
n1 = int(num1)
n2 = int(num2)
j -= 1
print(gcd(n1,n2))
except Exception as e:
pass
非常感謝 修改後成功通過了