[英]PSEUDO CODE & Python for finding the greatest, second greatest, and least number
[英]Python error while finding greatest number
我正在嘗試尋找3個數字最大的techgig問題。 問題如下
你只需要從stdin獲取三個數字作為輸入,你需要找到最好的數字。
輸入格式
您將分別從每行的stdin 1中取三個數字作為輸入。
約束
-100000 <= N <= 100000
輸出格式
您需要將三個最大的數字打印到標准輸出。
示例TestCase 1
輸入
902 100 666
我這樣做了
''' Read input from STDIN. Print your output to STDOUT '''
#Use input() to read input from STDIN and use print to write your output to STDOUT
import sys
def main():
s=sys.stdin.read()
s=s.split("\n")
a=int(s[0])
b=int(s[1])
c=int(s[2])
temp=0
e=[a,b,c]
for i in e:
if i > temp:
temp=i
print(temp)
'''if (a>b) and (a>c):
temp = a
elif (b>a) and (b>c):
temp = b
else:
temp = c
print(temp)'''
main()
它有預定義的輸入902,100,666。我的代碼顯示902輸出,預計也顯示902,但顯示失敗,為什么? 在上面的代碼注釋或非注釋事物都顯示失敗。
temp
被初始化為0,但輸入允許負數。 但是,0會被認為比任何負輸入都大。 這意味着對於具有負輸入的測試用例,您的算法將失敗。
將temp
初始化為小於允許的最小輸入的值:
temp = -100001 # One less than the minimum
我可能錯了,但輸出表示要使用標准輸出打印:您需要將三個數字中最大的一個打印到標准輸出。 python中的print與sys.stdout.write(str(99)+'\\ n')相同。 嘗試使用stdout打印結果。
sys.stdout.write(str(temp))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.