学习到了大大的 “print (sum())”
精彩的不要不要的{:5_91:}
我的答案
本帖最后由 天下独我意 于 2018-6-14 18:37 编辑>>> def oj(n):
opm=[]
if n%2 == 0:
for k in range(2,n+2,2):
opm.append(1/k)
return sum(opm)
else:
for k in range(1,n+2,2):
opm.append(1/k)
return sum(opm) def test15(n):
a=2
b=1
c=0
if n%2 ==0:
for a in range(n, 0, -2):
c =c + 1 / a
else:
for b in range(n, 0, -2):
c =c + 1 / b
print(c)
n= int(input('请输入一个正整数:'))
test15(n)
看看,还不会写 def get_value(n):
rlt=0
if n%2==0:
for i in range(1,int(n/2)+1):
rlt+=1/(2*i)
else:
for i in range(0,int((n+1)/2)):
rlt+=1/(2*i+1)
return(rlt)
print(get_value(8))
............ 刚学递归就用递归来算哈哈哈哈
def o(n):
if n == 0:
return 0
else:
return 1/n + o(n-2)
def d(n):
if n == 1:
return 1
else:
return 1/n +d(n-2)
n = eval(input('请输入一个数:'))
if n%2 == 0:
print(o(n))
else:
print(d(n)) 学习一下 def ds(n):
if n%2==0:
if n==2:
return 1/n
else:
return 1/n+ds(n-2)
else:
if n==1:
return 1
else:
return 1/n+ds(n-2) 看到第7集。。来凑个热闹。。。 a = int(input('请输入一个整数:'))
x = 0 #用于存储公式计算过程中的和
if a%2 ==0:
for b in range(2,a+1,2):
x += 1/b
print('1/2+1/4+...+1/n的和:',x,sep="")
else:
for c in range(1,a+1,2):
x += 1/c
print('1/1+1/3+...+1/n的和:',x,sep="") def result(n):
sum01 = 0
if n%2 != 0:
for i in range(1,n+1):
if i%2 != 0:
sum01 += (1/i)
else:
for i in range(1,n+1):
if i%2 == 0:
sum01 += (1/i)
print(sum01) def sum_15(n):
s=0
if n%2:
lst=list(range(1,n+1,2))
else:
lst=list(range(2,n+1,2))
for j in lst:
s+=1/j
return s
if __name__ == '__main__':
n = int(input('请输入数字:'))
print(sum_15(n)) def f_even(x):
result=0
for i in range(2,x+1,2):
result+=1/i
return result
def f_odd(x):
result=0
for i in range(1,x+1,2):
result+=1/i
return result
num=int(input('请输入一个整数:'))
if num%2:
print(f_odd(num))
else:
print(f_even(num)) a def fun(temp):
sum=0
while temp>0:
sum+=1/temp
temp-=2
return sum {:10_256:} def sum_n(n):
t=0
if n%2 == 0:
for i in range(2,n+1,2):
t+=1/i
else:
for i in range(1,n+1,2):
t+=1/i
return t