python 等差数列末项计算方式
作者:大叔小py 时间:2022-01-29 10:12:57
等差数列末项计算
题目内容:
给出一个等差数列的前两项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')
来源:https://blog.csdn.net/byakki/article/details/86522361
标签:python,等差数列,末项,计算
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python自动保存百度盘资源到百度盘中的实例代码
2023-12-18 07:05:01
![](https://img.aspxhome.com/file/2023/6/97276_0s.png)
如何使用python wasmtime调用rust生成的wasm库
2023-11-26 23:58:38
python基础之错误和异常处理
2021-07-28 05:46:15
![](https://img.aspxhome.com/file/2023/3/65933_0s.png)
学习ASP.NET八天入门:第六天
2007-08-07 13:48:00
![](https://img.aspxhome.com/file/UploadPic/20078/7/200787135226497s.gif)
基于Django与ajax之间的json传输方法
2023-01-12 19:52:58
Dreamweaver使用技巧--让css使网页图片半透明
2010-09-05 21:13:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723112956373s.jpg)
ASP在线生成电话图片程序
2007-10-06 23:06:00
![](https://img.aspxhome.com/file/UploadPic/200710/6/200710623165632s.gif)
Python源码学习之PyType_Type和PyBaseObject_Type详解
2023-08-03 15:50:05
![](https://img.aspxhome.com/file/2023/3/61733_0s.png)
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2022-06-09 18:52:34
![](https://img.aspxhome.com/file/2023/7/107207_0s.png)
宽屏不是用来阅读的
2009-04-05 15:59:00
python matplotlib实现条形图的填充效果
2022-03-12 07:55:20
![](https://img.aspxhome.com/file/2023/1/79561_0s.jpg)
Python Socket实现简单TCP Server/client功能示例
2021-05-21 19:19:46
SQL Server 压缩日志及数据库文件大小
2009-05-13 10:29:00
python 自动监控最新邮件并读取的操作
2023-02-04 12:58:51
Opera下的max-width BUG
2010-06-26 12:51:00
![](https://img.aspxhome.com/file/UploadPic/20106/26/2010626125629969s.png)
python使用__slots__让你的代码更加节省内存
2023-03-15 06:33:57
![](https://img.aspxhome.com/file/2023/5/77805_0s.jpg)
Python栈的实现方法示例【列表、单链表】
2023-07-20 15:51:42
关于Python中object类特殊方法的解释
2023-08-31 22:19:24
基于python图书馆管理系统设计实例详解
2023-06-28 23:44:13
![](https://img.aspxhome.com/file/2023/2/77782_0s.jpg)
浅谈Python用QQ邮箱发送邮件时授权码的问题
2021-10-30 13:06:59
![](https://img.aspxhome.com/file/2023/2/90942_0s.jpg)