格子林
发表于 2017-8-20 00:27:21
temp = input("请输入一个数字:")
n = int(temp)
if 0> n :
temp = input("请输入一个正整数:")
n = int(temp)
sum_all = 0
i = n%2
if i == 0:
i+=2
for j in range(i,n,2):
sum_all = sum_all + 1/j
print (sum_all)
木一
发表于 2017-8-22 21:58:27
def fun1(n):
result = 0
if n % 2 == 0:
i = 2
while i < n + 1:
result += 1 / i
i += 2
if n % 2 == 1:
i = 1
while i < n + 1:
result += 1 / i
i += 2
print(result){:10_243:}
木一
发表于 2017-8-22 21:59:18
冬雪雪冬 发表于 2017-4-1 19:54
写个简单的。
666666
yangzhijian
发表于 2017-8-23 16:32:11
{:5_95:}
morxrc
发表于 2017-8-27 14:32:20
print('_________每日一题15________')
def fuc(n):
result=0
if n%2==0:
for i in range(2,n+1,2):
result=result+1/i
print(result)
if n%2==1:
for i in range(1,n+1,2):
result=result+1/i
print(result)
princessjudy
发表于 2017-9-14 15:11:36
def ex15(number):
i = 0
if number%2 == 0:
strt = 2
else:
strt = 1
for x in range(strt,number+2,2):
i += 1/x
return i
张大象
发表于 2017-9-15 11:04:52
def sum1(n):
return sum(1/i for i in range(n,0,-2))
if __name__ == '__main__':
num = int(input('请输入一个数字:'))
print(sum1(num))
gausser
发表于 2017-9-16 22:50:20
from __future__ import division
def Fun_Main(n):
sum_value= 0
for i in range(n, 0, -2):
sum_value = sum_value + 1 / i
return sum_value
result = Fun_Main(8000)
print result
renze
发表于 2017-9-18 17:00:08
哇,头痛
vlomza123
发表于 2017-9-18 18:14:12
先看看先
aixuexi82
发表于 2017-9-28 14:30:47
def plus(num):
mylist = []
result = 0
if num % 2 == 0:
for i in range(1,(num+1)):
if i % 2 == 0:
mylist.append(i)
else:
for i in range(1,(num+1)):
if i % 2 == 1:
mylist.append(i)
for i in mylist:
result += 1/i
print(result)
num = int(input('请输入数字:'))
plus(num)
obuya1
发表于 2017-10-14 08:00:41
搞不懂
obuya1
发表于 2017-10-14 12:42:59
num = input('请输入一个数')
number = int(num)
sum = 0
if number % 2 == 1:
for i in range(2, n, 2):
sum += 1/i
else :
for i in range(1, n, 2):
sum += 1/i
print(sum)
朔夜
发表于 2017-10-17 15:21:10
def fun(temp):
sum = ''
if temp % 2 == 0:
for each in range(2, temp + 2, 2):
sum = sum + "+" + '1/' + str(each)
print(sum)
if temp % 2 != 0:
for each in range(3, temp + 2, 2):
sum = sum + "+" + '1/' + str(each)
print(sum)
if __name__ == '__main__':
temp = int(input("请输入一个数"))
fun(temp)
无边落木
发表于 2017-10-26 12:36:42
看看答案
lihw
发表于 2017-10-29 23:40:16
'''编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n'''
n = int(input("input number"))
def function_1(t):
b=0
for i in range (2,t+1,2):
b= 1/i+b
return(b)
def function_2(t):
b=0
for i in range (1,t+1,2):
b = 1/i+b
return(b)
if n%2==0:
print( function_1(n))
else:
print(function_2(n))
石小龙
发表于 2017-11-12 22:46:18
a = int(input('请输入一个整数'))
if a % 2 == 0:
sum1 = 0
while a - 2 > -1:
sum1 = sum1 + 1/a
a = a - 2
print(sum1)
else:
sum2 = 0
while a - 2 > -2:
sum2 = sum2 + 1/a
a = a - 2
print(sum2)
石小龙
发表于 2017-11-12 23:04:22
a = int(input('请输入一个整数'))
sum1 = 0
if a % 2 == 0:
n = -1
else:
n = -2
while a - 2 > n:
sum1 = sum1 + 1/a
a = a - 2
print(sum1)
优化了一下代码
你不知道我名字
发表于 2017-12-25 13:56:56
要查看代码
victor.xu
发表于 2018-3-3 17:20:16
def summ(n):
lis = []
if n % 2 == 0:
temp = int(n / 2)
for i in range(1, temp + 1):
lis.append(1 / (2 * i))
else:
temp = int((n + 1) / 2)
for i in range(0, temp):
lis.append(1 / (2 * i + 1))
result = sum(lis)
return lis, result
n1 = 11
(lis1, result1) = summ(n1)
print(lis1, result1)
n2 = 12
(lis2, result2) = summ(n2)
print(lis2, result2)
页:
1
2
3
4
[5]
6
7
8
9
10
11
12