wyf345491656 发表于 2018-3-20 23:48:35

inverseli 发表于 2018-3-20 22:15
第一项在上边,就是result哪里

计算结果是对的,但是print的结果里没有2/1这项

inverseli 发表于 2018-3-20 23:56:10

wyf345491656 发表于 2018-3-20 23:48
计算结果是对的,但是print的结果里没有2/1这项

哦哦{:10_333:}

夜与晨 发表于 2018-3-21 20:54:14

本帖最后由 夜与晨 于 2018-3-21 20:56 编辑

用了生成器和元组解包
def get_ab(num):
    i, a, b = 0, 1, 1
    while i <= num:
      a, b = b, a + b
      i = i + 1
      yield str(b) + '/' + str(a), float(b) / float(a)
def output():
    sum, num ,str_lv = 0, 19 ,[]
    for x , y in get_ab(num):
      sum = sum + y
      str_lv.append(x)
    return str_lv, sum

if __name__ == "__main__":
    m,n = output()
    print("%s = %.2f" % ('+'.join(m),n))

BlueSand 发表于 2018-3-21 20:57:26

fm = 1
fz = 2
sz = ""
result = 0.0
for i in range(1, 21):
    if sz == "":
      sz = sz+str(fz) + "/" + str(fm)
    else:
      sz = sz + "+" + str(fz) + "/" + str(fm)
    result = round(result + fz/fm, 3)
    temp = fz + fm
    fm = fz
    fz = temp

print(sz + "=" + str(round(result, 2)))

victor.xu 发表于 2018-3-22 11:37:58

def f(n):
    if n == 1:
      return 2
    elif n == 2:
      return 3
    else:
      return f(n - 1) + f(n - 2)
def ff(n):
    if n == 1:
      return 1
    elif n == 2:
      return 2
    else:
      return ff(n - 1) + ff(n - 2)
lis = []
st = ''
num = int(input('请输入自然数:\n'))
for i in range(1, num + 1):
    lis.append(f(i) / ff(i))
    st = st + str(f(i)) + '/' + str(ff(i)) + '+'
print('%s = %.2f'% (st[:-2], sum(lis)))

t6am3 发表于 2018-3-25 15:58:06

a1=1
a2=2
b1=1
b2=1
sum = 0
for i in range(20):
        a1 = a1 + a2
        tmp = a2
        a2 = a1
        a1 = tmp
        b1 = b1 + b2
        tmp = b2
        b2 = b1
        b1 = tmp
        print(str(a1)+'/'+str(b1), end='')
        if i != 19:
                        print('+',end='')
        sum += a1/b1       
print('=%0.2f' % sum)

Wangzy1025 发表于 2018-5-11 10:21:35

def feb():
    a, b = 0, 1
    while True:
      a, b = b, a+b
      yield b


def counter():
    a, b = feb(), feb()
    next(b)
    list1 = []
    temp = 0.0
    for i in range(20):
      i, j = next(b), next(a)
      list1.append('%d/%d' % (i, j))
      temp += i/j
    result = '%s = %.2f' % ('+'.join(list1), temp)
    return result

新手潘包邮 发表于 2018-5-12 07:21:42

def fun():
    a, b = 1, 1
    for i in range(20):
      a, b = b,a + b
      yield ("%s/%s"%(b,a))
sum_num = ""
for i in fun():
    sum_num += i +"+"
   
#sum_num = eval(sum_num[:-1])
print(sum_num[:-1]+" =","%.2f"%(eval(sum_num[:-1])))
print(sum_num[:-1]+" =",round((eval(sum_num[:-1])),2))

旅行, 发表于 2018-6-1 13:46:20

def fun():
    n = 1
    z = 2
    s = 0
    count = 20
    while count >0:
      s = (z/n) + s
      z,n= (n+z),z
      count -= 1
    print(s)
   
      

子沙 发表于 2018-10-10 21:42:39

def n0(num):
    x1,x2,times=0,1,1
    while times<num:
      x1,x2,times=x2,x1+x2,times+1
    return x2
def n1(num):
    a,b=n0(num+3),n0(num+2)
    return str(a)+'/'+str(b),a/b
def n(num):
    list1,result=[],0
    for i in range(0,num):
      list1.append(n1(i))
      result+=n1(i)
    print('+'.join(list1),end='=')
    print('%.2f'%result)
n(20)

小贤啊哦 发表于 2020-6-4 21:04:35

def fun114():
    s=0
    a=2
    b=1
    str1=''
    for i in range(1,21):
      if i!=0:
            str1+='+'
      
      str1+=str(a)+'/'+str(b)+'+'
      s+=a/b
      a,b=a+b,a
    print('%s'%(str1),'=',round(s,2))

kinkon 发表于 2022-9-7 16:15:35

本帖最后由 kinkon 于 2022-9-7 16:20 编辑

def f166(n):
    a = b = 1
    ans = 0
    arr = list()
    for i in range(n):
      s = '{}/{}'.format(a + b, b)
      ans += (a + b) / b      
      arr.append(s)
      a, b = b, a + b
      
    return '{0}={1:.2f}'.format('+'.join(arr), ans)

print(f166(20))
页: 1 2 [3]
查看完整版本: Python:每日一题 166