被翻红浪
发表于 2018-3-19 09:06:58
a = 2
b = 1
result = 0
for i in range(20):
if i < 19:
print("{}/{}+".format(a, b), end='')
else:
print("{}/{} = ".format(a, b), end='')
result += a/b
temp = a
a = a + b
b = temp
print(round(result, 2))
大头目
发表于 2018-3-19 09:47:35
str1 = ''
float1 = 0
def fib(max):
global str1
global float1
n, a, b = 0, 1, 1
while n < max:
a, b = b, a + b
n = n + 1
if n < 20:
float1 += b/a
str1 += str(b) + '/' + str(a) + '+'
if n == 20:
float1 += b/a
str1 += str(b) + '/' + str(a) + ' = ' + '%0.2f' %float1 #str(float1)
yield b/a
return 'done'
g = fib(20)
while True:
try:
x = next(g)
#print('g:', x)
except StopIteration as e:
#print('Generator return value:', e.value)
break
print(str1) #现有程序改编,丑是丑了点,仅解本题用
grf1973
发表于 2018-3-19 10:33:59
fib=
fib,fib=1,2
s,ss=2,'2/1'
for k in range(2,21):
fib=fib+fib
ss+='+'+str(fib)+'/'+str(fib)
s+=fib/fib
print(ss+'=',round(s,2))
jerryxjr1220
发表于 2018-3-19 10:59:26
def feb(s1=1,s2=1):
while True:
yield s2
s1, s2 = s2, s1+s2
a, b = feb(), feb(1,2)
s = "+".join()
print(s,"=","%.2f"%eval(s))
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
luokaoge
发表于 2018-3-19 11:03:56
本帖最后由 luokaoge 于 2018-3-19 11:05 编辑
x = 1
y = 1
a = ''
l = []
r = []
for i in range(1,21):
x , y = y , y + x
s = y/x
if i != 20:
a = str(y) + '/' + str(x) + '+'
else:
a = str(y) + '/' + str(x)
r.append(s)
l.append(a)
d = str(sum(r))
f = d[:5]
print("".join(l) + '=' + f)
lcfyujianwen
发表于 2018-3-19 13:25:51
def fun(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return (fun(n-1)+fun(n-2))
def summary(n):
sum = 0
for i in range(1, n+1):
sum += fun(i+1)/fun(i)
if i <= (n-1):
print (str(fun(i+1)) + '/' + str(fun(i)) + ' + ' , end = '')
else:
print (str(fun(i+1)) + '/' + str(fun(i)) + ' = ' , end = '')
print (('%.2f')%sum)
if __name__ == '__main__':
while True:
n = input('计算前n项的和(输入q退出计算):')
if n.isdigit():
summary(int(n))
elif n == 'q':
break
else:
print("输入错误,请重新输入一个正整数")
qpwoeiruty
发表于 2018-3-19 13:33:47
def SumFibonacci(num=20):
i=3
feibo =
a=1
b=1
j = 30
while i<=j:
c=a+b
i=i+1
feibo.append(c)
a=b
b=c
k=0
Result = 0
while k<=(num-1):
Result = Result + feibo/feibo
k=k+1
print ('Result='+('%.2f'%Result))
graceasyi
发表于 2018-3-19 13:40:37
本帖最后由 graceasyi 于 2018-3-19 13:41 编辑
def fib_itr(n):
if n < 0: return None
if n <= 1: return (0, 1)
a, b = 1, 1
for i in range(n-1):
a, b = b, a + b
yield a, b
sum = 0
str1 = ''
for i, j in fib_itr(21):
sum += j/i
str1 += str(j)+'/'+str(i)+'+'
print(str1[:-1], end='')
print("=%.2f" % sum)
结果:
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
benzhj
发表于 2018-3-19 15:35:51
本帖最后由 benzhj 于 2018-3-19 15:49 编辑
den =
num =
i = 0
temp = 1
result = 0
for i in range(18):
den.append(den + den)
num.append(num + num)
temp +=1
for i in range(len(den)):
result += num/den
for i in range(19):
print("{0}/{1}".format(num, den), end = '+')
print("{0}/{1}={2}".format(num, den, (int(result*100)/100)))
得数到是对了,但存在问题是,列表生成式,还是不会。。。。
还有保留两个小数,这个。。。我的解决方法应该不是python的解决方法。
期待别人的答案。
晓屁屁
发表于 2018-3-19 15:46:58
写的很low~~~~
list1 =[]
list2 =[]
z =0
for i in range(22):
if i ==0 or i ==1:
list1.append(1)
else:
list1.append(list1+list1)
try:
for x in range(len(list1)):
if x>1:
list2.append((int(list1)/int(list1)))
except IndexError:
pass
try:
for y in range(len(list1)):
z = float(list2) + z
except IndexError:
pass
try:
for a in range(len(list1)):
b = list1
print('%s/%s '%(str(list1),str(list1)),end='')
if str(list1) != '17711':
print(end='+ ')
if str(list1) == '17711' :
print(' =%.2f '%z)
continue
except IndexError:
pass
FTT100
发表于 2018-3-19 16:22:37
本帖最后由 FTT100 于 2018-3-19 16:31 编辑
def fab(n) :
a,b,c=0,1,1
result = []
result.append(1)
while c < n :
a,b = b,a+b
result.append(b)
c = c+1
return result
def listn(listn) :
a,b,c=0,0,1
pstr = ''
cnt=0
while c < len(listn)-1 :
if len(pstr) == 0 :
pstr=pstr+str(listn)+'/'+str(listn)
else :
pstr=pstr+'+'+str(listn)+'/'+str(listn)
cnt=cnt+round(listn/listn,4)
c = c+1
print(pstr+'=%0.2f'%(cnt))
rel = fab(22)
print(len(rel))
print(rel)
#rel=
listn(rel)
o41003121
发表于 2018-3-19 16:43:47
def fib(x):
if x == 1 or x == 2:
return 1
else:
return fib(x - 1) + fib (x - 2)
ssum = 0
sumstr = ''
for i in range(2,22):
ssum += fib(i + 1) / fib(i)
sumstr += str(fib(i+1))+'/'+str(fib(i))
if i != 21:
sumstr += '+'
else:
sumstr += '='
print(sumstr,'%.2f' % ssum)
idol
发表于 2018-3-19 16:49:59
listFib = []
listSum = []
def createFiblist(molecular, denominator, count):
if count > 0:
listFib.append(r'%s/%s'%(molecular, denominator))
listSum.append(molecular/denominator)
createFiblist(molecular+denominator, molecular, count-1)
return
def calcFib(list1):
result = 0
for i in range((len(list1)-1)):
print(list1 + '+', end=' ')
print(list1[-1]+ '=', end=' ')
for each in list1:
result += each
return '%.2f' % result
def main():
molecular = 2
denominator = 1
count = 20
list1 = createFiblist(molecular, denominator, count)
result = calcFib(list1)
print(result)
if __name__ == '__main__':
main()
yjsx86
发表于 2018-3-19 16:55:54
def func(times):
a, b = 2, 1
string = '2/1'
while times>1:
a, b = a+b, a
string += '+%d/%d' % (a,b)
times -= 1
print('%s = %.2f' % (string,eval(string)))
func(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
o41003121
发表于 2018-3-19 19:24:10
ssum = 0
sumstr = ''
alist =
for i in range(1,21):
alist.append(alist+alist)
ssum += alist / alist
sumstr += str(alist)+'/'+str(alist)
if i != 20:
sumstr += '+'
else:
sumstr += '='
print(sumstr,'%.2f' % ssum)
不用递归
wyp02033
发表于 2018-3-19 19:34:25
def fab(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return fab(n-1) + fab(n-2)
def main():
sum_ = 0
result = ''
for i in range(1, 21):
sum_ += fab(i+1) / fab(i)
result += (str(fab(i+1)) + "/" + str(fab(i)) + '+')
print(result[:-1] + "=%.2f"%(sum_))
if __name__ == '__main__':
main()
result: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
冬雪雪冬
发表于 2018-3-19 21:28:47
评分截至标记。
CH10
发表于 2018-3-19 21:46:25
def fibonacci(num):
a, b, result = 1, 1, 0
x, y = 0, 1
temp = ''
for i in range(num):
temp += str(a+b)+'/'+str(x+y)
if(i<num-1):
temp += '+'
result += (a+b)/(x+y)
c, z = a, x
a, x = b, y
b = c+b
y = z+y
temp += ('=' + str(round(result, 2)))
return temp
print(fibonacci(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
wyf345491656
发表于 2018-3-20 20:28:31
inverseli 发表于 2018-3-18 19:12
缺第一项
inverseli
发表于 2018-3-20 22:15:50
wyf345491656 发表于 2018-3-20 20:28
缺第一项
第一项在上边,就是result哪里