![f5b2d780d8168d64dd02defeb2da441e.png](https://i-blog.csdnimg.cn/blog_migrate/a16861f3ab6fafa51c4c5703b4422a74.jpeg)
等差数列末项计算
给出一个等差数列的前两项a1,a2,求第n项是多少
可以使用以下语句实现非负整数n的输入:
n=int(input())
输入格式:
三行,包含三个整数a1,a2,n
输出格式:
一个整数,即第n项的值
输入样例:
1
4
100
输出样例:
298
My answer
思路一:等差数列,先求差m是多少,第n项的值很多种方法算,我就采用这种a1 + m*(n-1)
a1 = int(input())
a2 = int(input())
m = a2 - a1
n = int(input())
N = a1 + m*(n-1)
print(N)
思路二:我们折腾一下电脑,让计算机用笨方法,从a1开始算,计算n-1次
a1 = int(input())
a2 = int(input())
n = int(input())
m = a2 - a1
for i in range(n-1):
a1 += m
print(a1)
python判断等差数列
代码:
import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline()
s = list(map(int, s.split(' ')))
print(n)
print(s)
for i in range(len(s)-1):
for j in range(i+1, len(s)):
if s[i] >= s[j]:
s[i], s[j] = s[j], s[i]
for j in range(1, len(s)-1):
if s[j] - s[j-1] == s[j+1] - s[j]:
flag = 1
else:
flag = 0
if flag == 1:
print('Possible')
else:
print('Impossible')