shencong132
发表于 2019-1-5 23:20:41
前来学习
hjx123hjx
发表于 2019-3-3 16:40:31
def func(n):
result = 0
if n % 2 == 0:
while n > 0:
result += float(1/n)
n -= 2
if n % 2 == 1:
while n > 0:
result += float(1/n)
n -= 2
return result
n = int(input())
print(func(n))
北城以北灬
发表于 2019-3-4 16:14:38
这道题的意思不是要编写3个函数吗?我记得之前学C的时候,这道题考的是函数的调用
def even(n):
sum = 0
for i in range(2, n+1, 2):
sum += 1/i
print("1/", i)
print(sum)
def odd(n):
sum = 0
for i in range(1, n+1, 2):
sum += 1/i
print("1/", i)
print(sum)
def ex15(n):
if n % 2 == 0:
even(n)
else:
odd(n)
大裤衩子
发表于 2019-3-15 11:41:52
来学习了,自己也写了个
def cla(n):
def loop(n):
s=0
for i in range(1,n+1):
s+=1/n
n-=2
if n<=0:
break
print(s)
if n % 2 == 0:
loop(n)
else:
loop(n)
aa=cla(int(input("输入一个数:")))
lwy520
发表于 2019-3-20 15:18:21
def jiouganshu(n):
outre=0
if n % 2:
for i in range(1,n+2,2):
outre += 1/i
else:
for i in range(2,n+2,2):
outre += 1/i
return outre
n=int(input("请输入一个正整数:"))
out_result=jiouganshu(n)
print(out_result)
yu123py
发表于 2019-4-28 10:33:40
def addmath():
n = int(input('请输入一个整数:'))
s = 0
if n % 2 == 0:
for i in range(2, n+1, 2):
s += 1/i
elif n % 2 == 1:
for j in range(1, n+1, 2):
s += 1/j
return s
山岂乎不在高
发表于 2019-7-8 16:02:45
本帖最后由 山岂乎不在高 于 2019-7-8 16:07 编辑
n = int(input('请输入一个正整数:'))
result = 1/n
while n-2 >= 1:
n -=2
result += 1/n
print("结果是:",result)
13825729116
发表于 2019-8-2 21:47:20
Python:每日一题 15
panheng
发表于 2019-8-12 19:20:28
测试了下,递归到2000位就超出最大运算了。
# 题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
# 解法1:定义函数answer1,采用递归方法求解
def answer1(n):
if n == 1:
return 1
elif n == 2:
return 1 / 2
else:
return answer1(n-2) + 1 / n
# 解法2,采用迭代方法求解
def answer2(n):
result = 0
while n > 0:
result += 1 / n
n -= 2
return result
if __name__ == '__main__':
n = int(input("请输入一个正整数:"))
print(answer1(n))
print("-------------")
print(answer2(n))
克里斯保罗
发表于 2019-8-28 22:40:02
噢噢~def test(n):
result = 1
if n%2:
for each in range(3,n+1,2):
result += 1/each
else:
result =1
for each in range(2,n+1,1):
result += 1/each
return result
def main():
n = int(input('请输入需要加工的n:'))
print('%f'%(test(n)))
if __name__ == '__main__':
main()
阿瑞
发表于 2019-9-11 20:10:17
S = 0
n = int(input('请输入一个数'))
if n % 2 == 0:
for i in range(1,int(n/2+1)):
S += 1/(2*i)
print(S)
if n % 2 == 1:
for i in range(1,int((n+1)/2+1)):
S += 1/(2*i-1)
print(S)
191820061
发表于 2019-11-2 22:59:29
看看
tuqishan8023
发表于 2019-11-21 15:23:47
def test15(n):
s = 0
if n%2 ==0:
for i in range(1,n+1):
if i%2 ==0:
num = 1/i
s =s + num
print(s)
else:
for i in range(1,n+1):
if i%2 !=0:
num = 1/i
s =s + num
print(s)
if __name__ == '__main__':
test15(49)
xxwoaini897
发表于 2019-11-21 17:01:39
学习
Jung
发表于 2019-11-26 10:58:27
def fun15():
N = int(input("Please enter a number:"))
Sum = 0
if N%2==0:
Sum =sum(1/(2*i) for i in range(1,(N//2)+1)):
else:
Sum = sum(1/(2*i+1) for i in range((N//2)+1)):
return Sum
九五二七
发表于 2019-12-11 20:57:21
n=int(input('please input a number:'))
sum=0
def Cal(a):
global sum
if a % 2 == 0:
for i in range(2,a+2,2):
print(i)
sum=sum+(1/i)
else:
for i in range(1, a + 1, 2):
print(i)
sum = sum + (1 / i)
return sum
print(Cal(n))
九五二七
发表于 2019-12-11 21:00:43
九五二七 发表于 2019-12-11 20:57
n=int(input('please input a number:'))
sum=0
def Cal(a):
看大神的,精简下:
n=int(input('please input a number:'))
sum=0
def Cal(a):
global sum
for i in range(a, 0, -2):
print(i)
sum = sum + (1 / i)
return sum
print(Cal(n))
18463102026
发表于 2019-12-11 23:24:47
新手·ing 发表于 2017-4-1 18:44
@lumber2388779 @ooxx7788 @冬雪雪冬 @jerryxjr1220
来吧
这里有几点题目不是很明确,比如n可以是负数吗?可以为0吗?因为有一种说法是0也是偶数
18463102026
发表于 2019-12-11 23:30:50
from functools import lru_cache
#输入n为正整数
@lru_cache()
def fun(n):
if n==1 :
return 1
elif n==2:
return 1/2
else :
return 1/n+fun(n-2)
verauxu
发表于 2019-12-15 21:18:06
def fn(n):
sum1=0#偶数和
sum2=0
if n % 2 == 0:
for n in range(2,n+1,2):
sum1 += 1/n
return sum1
else:
for n in range(1,n+1,2):
sum2 += 1/n
return sum2
页:
1
2
3
4
5
6
7
8
[9]
10
11
12