Python:每日一题 166
本帖最后由 冬雪雪冬 于 2018-3-19 21:12 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
本题由 @shigure_takimi 提供,有一分数序列:2/1,3/2,5/3,8/5,8/13,21/13...求出这个序列的前20项之和。
以字符串 2/1+3/2+5/3+8/5+13/8+21/13+……= result 的形式返回结果。要求结果保留2位小数。
输出范例:
2/1+3/2+5/3+8/5+13/8+21/13+34/21+55/34+89/55+144/89+233/144+377/233+610/377+987/610+1597/987+2584/1597+4181/2584+6765/4181+10946/6765+17711/10946 = 32.66
提示:很容易看出这是用到了斐波那契数列,可以定义一个函数反复调用,但每次调用都从1开始计算,效率很低,可以采用生成器或直接在主程序中计算。
感谢 @shigure_takimi 提供题目,也希望大家有适宜的题目提供给我们,共同把每日一题这个项目做好。请 @shigure_takimi 多回一帖,给予出题奖励。
要求:
题目难易适度,以python语言的运用为主,算法为辅。
对于提供优质题目的鱼油给予鱼币奖励。
可以发私信给我或直接发帖并@我。 本帖最后由 Chase_Kas 于 2018-4-4 20:14 编辑
学完生成器了~~
def fibs():
a, b = 1, 1
while True:
a, b = b, a+b
yield (a, b)
def fun(times):
sums, i, equation = 0, 1, ''
for each in fibs():
if i<times:
equation += '{0}/{1}+'.format(each,each)
sums += (each/each)
i += 1
else:
sums += (each/each)
equation += '{0}/{1}={2:.2f}'.format(each,each,sums)
break
print(equation)
fun(20) def sum(n):
if n==1:
return 1
if n==2:
return 2
else:
return sum(n-1)+sum(n-2)
sum0 = 0
for i in range(2,22):
sum1 = sum(i)/sum(i-1)
sum0 += sum1
if i == 21:
print(r'%d/%d='%(sum(i),sum(i-1)),end='')
else:
print(r'%d/%d+'%(sum(i),sum(i-1)),end='')
print('%.2f'%sum0)
亲测可行,和要求一样
谢谢提示。
PS:只能以此拙劣的方式去除最后面的+...
def Fibo_iter(n):
if n == 'den':
a, b = 1, 1
elif n == 'mol':
a, b = 1, 2
for i in range(20):
a, b = b, a + b
yield a
def main():
reslut_str = ''
reslut_int = 0
denominator = Fibo_iter('den')#分母
molecule = Fibo_iter('mol')#分子
for i, j in zip(denominator, molecule):
reslut_int = reslut_int + j / i
reslut_str += str(j) + '/' + str(i) + '+'
print(reslut_str[:len(reslut_str)-1],'=',round(reslut_int,2))
if __name__ == '__main__':
main() #领奖咯!
def f(n):
if n < 1:
return None
else:
fenzi = 2
fenmu = 1
s = []
total = 0
for i in range(n):
total += fenzi/fenmu
s.append(str(fenzi)+'/'+str(fenmu))
fenzi, fenmu = fenzi+fenmu, fenzi
return '+'.join(s)+'='+'%.2f' % total
print(f(1))
print(f(20)) #分母
fm = 1
#分子
fz = 2
result = fz/fm
for i in range(1,20):
temp = fz
fz = fm +fz
fm = temp
result += fz/fm
if i == 19:
print(str(fz)+"/" +str(fm)+" = "+str(round(result,2)),end=' ')
else:
print(str(fz)+"/" +str(fm)+"+",end=' ')
本帖最后由 阿bang 于 2018-3-18 19:25 编辑
fibo =
for i in range(20):
fibo.append(fibo + fibo)
output_str = ''
result = 0
for j in range(20):
output_str += str(fibo) + '/' + str(fibo)
if j < 19:
output_str += '+'
result += float(fibo) / fibo
print output_str, '=', '%.2f' % result def fib():
x = 0
y = 1
while True:
x,y = y,x+y
yield x
list1 = []#前20项的分子
list2 = []#前20项的分母
count = 0
result = 0
for each in fib():
if count > 21:
break
else:
if 0 < count <= 20:
list2.append(each)
if 1 < count <= 21:
list1.append(each)
count += 1
for i in range(20):
result += list1/list2
if i != 19:
print(str(list1) + '/' + str(list2) + '+',end = '')
else:
print(str(list1) + '/' + str(list2) + ' = ',end = '')
print('%.2f' %result) EVA{:10_256:}
s,a,b=0,1,2
e=''
for i in range(20):
s+=b/a
e+=(str(b)+'/'+str(a)+'+')
a,b=b,a+b
c=e[:-1]
print('%s=%.2f'%(c,eval(c)))
生成器?
def fibs():
s,a,b,t=0,1,1,0
while True:
a,b=b,a+b
s+=b/a
t+=1
yield b,a,s,t
for x in fibs():
if x<20:
print('%d/%d+'%x[:2],end='')
if x==20:
print('%d/%d=%.2f'%x[:3])
break 本帖最后由 Agoni 于 2018-3-18 20:16 编辑
def fab(n):
f1 = 1
f2 = 1
f3 = 0
for i in range(n):
f1 = f1 + f2
f3 = f1 / f2 + f3
f2 = f1 + f2
f3 = f2/ f1 + f3
return '%.2f'% f3
print(fab(10)) def generate_fib_array(num):#传入的参数表示求和项数
fib =
for i in range(2, num+1):
fib.append(fib+fib)#这里一开始写成了fib(i-1),报错'list' object is not callable,该打。
return fib
def cal_frac_sum(fib, num):#计算分数的和,传入参数为一个菲波那切数列以及求和项数
__sum = 0
for i in range(num):
__sum += fib/fib
return __sum
print('%.2f' % cal_frac_sum(generate_fib_array(20), 20))
def new_fib(number):
deno = []
deno.append(1)
deno.append(2)
for i in range(2,number):
deno.append(deno + deno)
nume = []
nume.append(2)
nume.append(3)
for i in range(2,number):
nume.append(nume + nume)
result = 0
for i in range(number):
result += nume/deno
for i in range(number):
print(str(nume)+'/'+str(deno),end = '')
if i+1 == number:
break
else:
print('+',end = '')
print('=%.2f'%result)
if __name__ == '__main__':
number = int(input('请输入要计算多少位:'))
new_fib(number)
结果:
请输入要计算多少位:20
2/1+3/2+5/3+8/5+13/8+21/13+34/21+55/34+89/55+144/89+233/144+377/233+610/377+987/610+1597/987+2584/1597+4181/2584+6765/4181+10946/6765+17711/10946=32.66 水平有限,找个难度适中的题还挺不容易。 a=1 ; b=2 ; s1=
for i in range(2,24):
s1+=
a=b
b=s1
s2='' ; s3=0
for i in range(0,20):
s2+='+'+str(s1)+'/'+str(s1)
s3+=s1/s1
s2=s2+' = '+'%3.2f'%s3
print(s2) def fun_z(n):
a = 1
b = 1
for i in range(n):
a, b = b, a+b
yield b
def fun_m(n):
a = 0
b = 1
for i in range(n):
a, b = b, a+b
yield b
def main():
result = 0
qq = []
pp = []
count = 1
n = ''
for i in fun_z(20):
qq.append(i)
for m in fun_m(20):
pp.append(m)
for x in range(20):
result += qq/pp
n += str(qq) + '/' + str(pp) + '+'
print('%s = %.2f' %(n[:-2], result))
if __name__ == '__main__':
main()
本帖最后由 天圆突破 于 2018-3-18 22:44 编辑
def summ(s = 20):
a, b, string = 1, 1, ''
for i in range(s):
a, b = b, a+b
string+= '%s%s%s%s'%(str(b),'/',str(a),(i!=s-1)*'+')
return '%s%s%.2f'%(string,' = ',eval(string))
if __name__ == '__main__':
print(summ(20)) 应该是太复杂了...
#有一分数序列:2/1,3/2,5/3,8/5,8/13,21/13...求出这个序列的前20项之和。
#以字符串 2/1+3/2+5/3+8/5+13/8+21/13+……= result 的形式返回结果。
#要求结果保留2位小数。
#创建一个生成各个分数及其值字典的生成器
def gfs():
m=2
n=1
strkey=str(m)+'/'+str(n)
jgv=m/n
mydc={strkey:jgv}
yield mydc
while True:
k=m
m=m+n
n=k
strkey=str(m)+'/'+str(n)
jgv=m/n
mydc={strkey:jgv}
yield mydc
def func(n):
stest=gfs()
prnstr=''
prnnum=0.00
counter=0
while counter<n:
cn=next(stest)
if counter==n or counter==0:
prnstr=prnstr+list(cn.keys())
else:
prnstr=prnstr+'+'+list(cn.keys())
if counter>1 and counter%5==0:
prnstr=prnstr+'\n'
prnnum=prnnum+list(cn.values())
counter=counter+1
print(prnstr+'=%.2f'% prnnum)
func(20)
def fibo(n):
if n <= 1:
return n
else:
return(fibo(n-1) + fibo(n-2))
list1=
list2=[(str(fibo(i)) + '/' + str(fibo(i-1))) for i in range(3,23) ]
M='+'.join(list2)
N='%.2f' % sum(list1)
print('{}={}'.format(M,N)) def fun(n):
str1 = ''
a = 1
b = 2
c = 0
num = 0
for i in range(n):
if i != 0:
str1 += '+'
str1 += str(b) + '/' + str(a)
num += b/a
c = a + b
a = b
b = c
print('%s' %(str1) ,'=','%.2f' %(num))
fun(20)
def Fib():
a, b = 1, 2
sum = 0
result = ''
while True:
sum += b/a
result += '%s/%s+' %(b, a)
a, b = b, a+b
yield '%s = %0.2f'%(result[:-1], sum)
fib = Fib()
for i in range(19):
next(fib)
print(next(fib))