【Python練習】重做王道機試1

分段函數

m = int(input())
for i in range(m):
    x = int(input())
    if 0 <= x < 2:
        print("y=%.1f" %(-x+2.5))
    elif 2 <= x < 4:
        print("y=%.1f" %(2-1.5*(x-3)*(x-3)))
    elif 4 <= x < 6:
        print("y=%.1f" %(x/2-1.5))

反序輸出

while True:
    try:
        x = input()
        for i in range(len(x)):
            print(x[len(x) - i - 1], end = "")
    except:
        break
    print()

成績排序

temp = []
N = int(input())
for i in range(N):
    temp.append(input().split())
    temp[i][1],temp[i][2] = int(temp[i][1]),int(temp[i][2])
temp.sort(key=lambda x:(x[2],x[0],x[1]))
for i in range(N):
    for j in range(3):
        print(temp[i][j],end=" ")
    print()

查找

while True:
    try:
        a,b,c,d = input(),list(map(int,input().split())),input(),list(map(int,input().split()))
        for i in d:
            print("YES" if i in b else "NO")
    except:
        break

堆棧的使用

while True:
    try:
        temp = []
        x = int(input())
        while x:
            y = input().split()
            if y[0] == "P":
                temp.append(y[1])
            elif y[0] == "O" and len(temp) > 0:
                temp.pop()
            elif y[0] == "A":
                if(len(temp) > 0):
                    print(temp[len(temp) - 1])
                else:
                    print("E")
            x -= 1
        print()
    except:
        break
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章