1. 給定無序整數序列,求連續非空子串最大和,例如{-23 17 -7 11 -2 1 -34},子串為{17,-7,11},最大和為21
輸入描述:
輸入為整數序列,數字用空格分隔,如:-23 17 -7 11 -2 1 -34
輸出描述:
輸出為子序列的最大和:21
輸入例子1:
-23 17 -7 11 -2 1 -34
輸出例子1:
21
Python
arr = list(map(int, input().split()))
temp = arr[0]
max1 = arr[0]
for i in range(1,len(arr)):
temp = temp + arr[i]
if arr[i] > temp:
temp = arr[i]
if temp > max1:
max1 = temp
print(max1)
2. 給定無序整數序列,求其中第K大的數,例如{45,67,33,21},第2大數為45
輸入描述:
輸入第一行為整數序列,數字用空格分隔,如:45 67 33 21
輸入第二行一個整數K,K在數組長度範圍内,如:2
輸出描述:
輸出第K大的數,本例為第2大數:45
輸入例子1:
45 67 33 21
2
輸出例子1:
45
Python
arr = list(map(int, input().split()))
k = int(input())
arr.sort(reverse=True)
print(arr[k-1])