liujian973
发表于 2018-9-13 22:38:51
def function(n):
temp=0
for i in range(,n+1,2):
temp +=1/i
print("temp=1/{}".format(i))
print(temp)
lyslyslys
发表于 2018-9-18 13:34:47
感谢楼主
没有土豆的洋芋
发表于 2018-9-20 20:14:49
import random
a=random.randint(0,99)
if a%2==0:
b=int(a/2)
print(b)
sum=0
for i in range(b):
sum+=1/2**i
else:
c=int((a+1)/2)
sum=0
for i in range(1,c):
sum+=1/((2**i)-1)
print(sum)
阿磕磕
发表于 2018-9-30 11:20:04
1
阿磕磕
发表于 2018-9-30 11:23:14
kan
122815306
发表于 2018-9-30 11:49:07
def o_n(n):
sum = 0
for i in range(2,n+1,2):
sum += 1/i
return sum
def j_n(n):
sum = 0
for i in range(1,n+1,2):
sum += 1/i
return sum
while True:
n = int(input('请输入一个数:'))
if n % 2 == 0:
print(o_n(n))
else:
print(j_n(n))
if n == 0:
break
lwhq
发表于 2018-10-14 16:52:09
1
liujian973
发表于 2018-11-23 15:10:04
本帖最后由 liujian973 于 2018-11-23 15:15 编辑
def foo_ongs(n):
return sum(,n+1,2)])
double-lee
发表于 2018-12-5 00:16:37
def judgeNum(n):
total = 0.0
if n % 2 == 0:
for i in range(2,n+1,2):
total += 1/i
return total
else:
for i in range(1,n+1,2):
total += 1/i
return total
n = int(input("输入一个正整数: "))
print(judgeNum(n))
HUMMER军
发表于 2018-12-5 02:11:19
def fun(n):
if n%2==0:
if n==2:
return 1/2
else:
return 1/n+fun(n-2)
else:
if n==1:
return 1
else:
return 1/n+fun(n-2)
n=int(input('请输入一个整数:'))
print(fun(n))
爱学习的喵
发表于 2018-12-5 10:05:09
def func1(n):
if n==1 or n==2:
return 1/n
else:
return 1/n + func1(n-2)
print(func1(3))
我的做法,比较传统,大神见笑了
Y0uWill_1Will
发表于 2018-12-5 10:26:43
ok
兜兜爱python
发表于 2018-12-6 21:07:37
本帖最后由 兜兜爱python 于 2018-12-6 21:22 编辑
def fun(n):
sum=0
if n%2==0:
for i in range(2,n+1,2):
sum+=1/i
else:
for j in range(1,n+1,2):
sum+=1/j
return sum
a=int(input("请输入一个数字:"))
b=fun(a)
print(b)
15046058134
发表于 2018-12-14 18:34:08
感谢
zclsgl409945033
发表于 2018-12-14 19:22:05
做题
fullingzhn520
发表于 2018-12-14 20:24:06
def sum1(a):
if (a%2 == 0):
sum2 = 0
for i in range(0, int(a/2)):
#print(i)
sum2 = sum2 + 1/(2*(i+1))
#print(sum2)
return(sum2)
elif (a%2 == 1):
sum2 = 0
for i in range(0, int((a+1)/2)):
#print(i)
sum2 = sum2 + 1/(2*i+1)
#print(sum2)
return(sum2)
while True:
n = int(input('请输入一个整数:'))
if n != 0:
print(sum1(n))
else:
print('请输入非零整数,谢谢!')
dxyxln
发表于 2018-12-17 11:00:22
>>> def even_sum(n):
s1 = 0
for i in range(2, n+1, 2):
s1 += 1.0 / i
return s1
>>> def odd_sum(n):
s2 = 0
for i in range(1, n+1, 2):
s2+= 1.0 / i
return s2
>>> def my_sum(n):
if n % 2 == 0:
s = even_sum(n)
else :
s = odd_sum(n)
print(s)
dxyxln
发表于 2018-12-17 11:03:20
修改一波
def even_sum(n):
s1 = 0
for i in range(2, n+1, 2):
s1 += 1.0 / i
return s1
def odd_sum(n):
s2 = 0
for i in range(1, n+1, 2):
s2+= 1.0 / i
return s2
def my_sum(n):
if n % 2 == 0:
s = even_sum(n)
else :
s = odd_sum(n)
return s
if __name__ == '__main__':
n = int(input('input a number:\n'))
sum = my_sum(n)
print(sum)
Kan丶
发表于 2018-12-18 21:55:09
递归直接出来
咕咕鸡鸽鸽
发表于 2019-1-4 09:11:49
kankan
页:
1
2
3
4
5
6
7
[8]
9
10
11
12