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])