每日codingame小游戏练习[2021.4.06](python3入门学习之等比/等差数列)

题目描述

Find the N th term
given arithmetic or geometric sequence s.
Input:
An integer N
A space-separated line of integers representing the first 6 numbers of the sequence s.
output:
The N th term of the sequence as an integer . Note that the sequence is O-indexed !
eg:

inputouput
7
1 2 3 4 5 6
8

题目分析

题目要求我们先输入一个整数N,接着输入一个等差或等比数列,输出这个数列的第N项。

解题代码

一位来自葡萄牙老哥的代码:

n,a,b,c,*s=map(int,open(0).read().split())
print([(b-a)*n+a,int(a*(b/a)**n)][c-b!=b-a])

总结

  1. open(0).read().split()可以理解为input().split(),但使用open可以自由的控制输入,按Ctrl+Z即可关闭输入,如果用input(),需要用两个才能实现。

  2. python3 文件操作open() 方法超全详解:open(0)表示当buffering设置为0时,即表示不使用缓冲,直接进行读写。

  3. Python map() 函数:map() 会根据提供的函数对指定序列做映射。

    第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

  4. python基础:list变量前面加星号,字典变量前面加两个星号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值