Calculation of the last term of the arithmetic sequence
Subject content:
Given the first two terms a1 and a2 of an arithmetic sequence, find the nth term
You can use the following statement to achieve the input of a non-negative integer n:
n=int(input())
**Input format: **
Three lines, containing three integers a1, a2, n
**Output format: **
An integer, the value of the nth term
Input sample:
1
4
100
**Sample output: **
298
My answer
*Idea 1: ** Arithmetic sequence, first find the difference m, there are many ways to calculate the value of the nth term, I use this a1 + m(n-1)
a1 =int(input())
a2 =int(input())
m = a2 - a1
n =int(input())
N = a1 + m*(n-1)print(N)
**Idea 2: **Let's toss the computer, let the computer use the dumb method, start counting from a1, and calculate n-1 times
a1 =int(input())
a2 =int(input())
n =int(input())
m = a2 - a1
for i inrange(n-1):
a1 += m
print(a1)
Supplementary knowledge: python judgment arithmetic sequence
Not much nonsense, let's just look at the code!
import sys
n =int(sys.stdin.readline().strip())
s = sys.stdin.readline()
s =list(map(int, s.split(' ')))print(n)print(s)for i inrange(len(s)-1):for j inrange(i+1,len(s)):if s[i]= s[j]:
s[i], s[j]= s[j], s[i]for j inrange(1,len(s)-1):if s[j]- s[j-1]== s[j+1]- s[j]:
flag =1else:
flag =0if flag ==1:print('Possible')else:print('Impossible')
The calculation method of the last item of the above python arithmetic sequence is all the content shared by the editor, I hope to give you a reference.
Recommended Posts