Python|PTA "Simple Function Operation"

Problem Description

  1. Enter a positive integer m (20<=m<=100) to calculate the value of 11+12+13+...+m.

  2. This topic requires calculating the value of the following piecewise function f(x):

In one line, output in the format of "f(x) = result", in which x and result both retain one decimal place.

solution

  1. First, prepare the python environment, the editor runs in pycharm

  2. First of all, for problem 1, this is a simple cumulative summation problem but the problem requires the range of m, we use the for loop to solve

The results obtained are as follows:

m=int(input())if m>=20 and m<=100:     S =0for i inrange(11,m+1):         S+=i         sum=S     print("sum =",S)

What is worth noting is the value of m, because PTA has very strict requirements on the format of the problem-solving process, so an extra space should be added to the final answer.

For question 2, this question is a classic if judgment question. The exercise of this question can enhance the use of if statements. This question needs to pay attention to the formatting of f(x) = result and the use of result decimals. This can be solved with a %s placeholder:

a=int(input())if a !=0:     result=1/a     print('f(%.1f) = %.1f'%(a, result))else:print('f(%.1f) = %.1f'%(a, a))

Conclusion

The learning of Python is endless. Diligent practice every day can always benefit people. No matter the problem is easy or difficult, you can enjoy the happiness and consolidate knowledge from doing the problem.

END

Recommended Posts

Python|PTA &quot;Simple Function Operation&quot;