sys方法 vs input()方法
輸出結果都正確請問為甚麼第一個方法錯誤
from math import gcd from sys import stdin for s in stdin: a,b=map(int,s.split()) print(gcd(a,b))
-------------------------------------------------------
import math while True: try: x, y = [int(i) for i in input().split()] print(math.gcd(x, y)) except: break
sys方法 vs input()方法
輸出結果都正確請問為甚麼第一個方法錯誤
from math import gcd from sys import stdin for s in stdin: a,b=map(int,s.split()) print(gcd(a,b))
-------------------------------------------------------import math while True: try: x, y = [int(i) for i in input().split()] print(math.gcd(x, y)) except: break
抱歉第二個應該是
import math while True: try: x, y = [int(i) for i in input().split()] print(math.gcd(x, y)) except: break
那不是 sys module 和 input 的差異
而是你的程式結構的差異。
這題測資後面有空白行,
空白行要切為 a, b 會出錯。
你用 try ... except ...
遇到錯誤就終止了。
建議別用大迴圈加 try ... except ...
這樣根本不知錯在哪裡。