Python solves the characteristics of the sequence

Questions basic training series features

Resource limit
Time limit: 1.0s Memory limit: 256.0MB
Problem description
Given n numbers, find the maximum, minimum, and sum of these n numbers.

Input format The
first line is an integer n, which represents the number of numbers.

The second line has n numbers, which are given n numbers, and the absolute value of each number is less than 10000.

Output format
Output three lines, each line an integer. The first line represents the maximum of these numbers, the second line represents the minimum of these numbers, and the third line represents the sum of these numbers.
Sample input
5
1 3 -2 4 5
Sample output
5
-2
11
Data scale and convention
1 <= n <= 10000.

Two solutions are provided below:
Solution 1 is solved by loop and judgment:

n=int(input())
s=[int(i) for i in input().split()]
t=-9999
r=9999
m=0
for i in range(n):
    if s[i]>t:  #比较计算最大值
        t=s[i]
    if s[i]<r:  #比较计算最小值
        r=s[i]
    m+=s[i]
print(t)
print(r)
print(m)

Option two adopts the python built-in function map() and the related functions for processing numeric lists:

n=int(input())
x=list(map(int,input().split()))
print(max(x))
print(min(x))
print(sum(x))

Guess you like

Origin blog.csdn.net/qq_45701131/article/details/104578118