尔东方
发表于 2020-4-30 14:28:29
def f15(n,x):
if n % 2 == 0:
for i in range(2,n+1,2):
x=x+1/i
print(x)
if n%2!=0:
for i in range(2,n+1,1):
x=x+1/i
print(x)
if __name__=='__main__':
n=int(input("输入一个数:"))
f15(n,0)
Balaenopter
发表于 2020-5-3 08:15:24
def recursive(num):
if num == 2:
return 1.0 / 2
elif num == 1:
return 1.0
else:
return even(num - 2) + 1.0 / num
num = int(input('请输入一个数字:'))
print(recursive(num))
土二奇
发表于 2020-5-4 10:50:06
from fractions import Fraction
def fsum(n):
a=0
b=0
for i in range(1,int(n/2+1)):
a=Fraction(1,2*i)
b+=a
return b
def esum(n):
a=0
b=0
for i in range(0,int(n/2+1)):
a=Fraction(1,2*i+1)
b+=a
return b
x=int(input("请输入一个整数:"))
if x%2==0:
print(fsum(x))
else:
print(esum(x))
san1412
发表于 2020-5-15 08:53:32
from fractions import Fraction #引入模块
def a_function(n):
result = 0
if n % 2 == 0:
for i in range(2,n+1,2):
result += Fraction(1,i)
print(Fraction(1,i),end="")
if i != n:
print("+",end="")
else:
for i in range(1,n+1,2):
result += Fraction(1,i)
print(Fraction(1,i),end="")
if i != n:
print("+",end="")
print(' = ',end='')
return (result)
num = int(input("请输入一个正整数:"))
print(a_function(num))
19971023
发表于 2020-5-26 14:04:03
1
bujust_h
发表于 2020-5-26 16:45:36
n = int(input('请输入任意整数:'))
sum = 0
if n % 2 ==0:
for i in range(2,n+1,2):
sum += 1/i
else:
for i in range(1, n+1, 2):
sum+= 1/i
print(sum)
rushpy
发表于 2020-5-26 18:34:52
{:5_109:}
ForPorsche
发表于 2020-5-26 22:49:03
def num(n):
s = 0
if n % 2 == 0:
list1 = list(range(2, n + 1, 2))
list2 =
print('%.2f' % sum(list2))
else:
list1 = list(range(1, n + 1, 2))
list2 =
print('%.2f' % sum(list2))
num(int(input('输入一个数:')))
nononoyes
发表于 2020-6-1 16:15:39
'''
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
知识点:整数变为字符串:str();字符串拼接;去掉最后一个字符str[:-1]
'''
result = ""
def isEven(n):
global result
if(n % 2 == 0):
for i in range(2,n+1):
if i % 2 == 0:
result += "1"+"/"+str(i)+"+"
else:
for i in range(1,n+1):
if i %2 !=0:
result += "1"+"/"+str(i)+"+"
return result
n = int(input("请输入一个数字:"))
s = isEven(n)
s = str(n)+"="+s
print(s[:-1])
请输入一个数字:9
9=1/1+1/3+1/5+1/7+1/9
素手就琴
发表于 2020-6-8 22:49:00
def fun(n):
if n%2 == 0:
return fun1(n)
elif n%2 == 1:
return fun2(n)
def fun1(m):
i = 1
result = 0
while i <= m//2:
result += 1/(i*2)
i += 1
return result
def fun2(p):
i = 0
result = 0
while i <= (p-1)//2:
result += 1/(i*2+1)
i += 1
return result
num = int(input("请输入一个整数:"))
print(fun(num))
junsitu9159
发表于 2020-6-11 04:48:01
def num(n):
sum = 0
if n <= 0:
print("input error")
elif n%2 == 0:
for i in range(2,n+2,2):
s = 1/i
sum += s
print(sum)
else:
for i in range(1,n+2,2):
s =1/i
sum += s
print(sum)
今天的我更强了
发表于 2020-6-27 08:45:31
{:10_249:}
鲨鱼辣椒1
发表于 2020-8-9 10:33:30
def call(n):
s = 0
if n % 2 == 0:# (偶数)
for i in range(2,n+1,2):
s = s + 1/i
print(s)
elif n % 2 == 1:
for i in range(1,n+1,2):
s = s + 1/i
print(s)
else:
print("输入的不是整数,请重新输入:")
while True:
a = input("您要计算吗?(YES/NO)")
if a == "YES":
call(int(input("请输入一个整数:")))
else:
print("感谢使用。")
break
281277
发表于 2020-8-9 11:37:18
def a_sum(n):
n_sum = 0
for i in range(1, int(n / 2 + 1)):
n_sum += 1 / (2 * i)
return n_sum
def b_sum(n):
n_sum = 0
for i in range(1, int(n / 2 + 1)):
n_sum += 1 / (2 * i - 1)
return n_sum
if __name__ == '__main__':
n = int(input("请输入正整数n:"))
if n % 2 == 0:
print(a_sum(n))
else:
print(b_sum(n))
BSOD
发表于 2020-8-9 14:35:53
n = 10
print(sum())
hshb875
发表于 2020-8-9 17:54:42
!!!!!!!!!!!
sanchuan555
发表于 2020-12-15 19:33:30
def countA(n):
a=0
if n%2==0:
for i in range(2,n+1,2):
a=a+1/i
else:
for i in range(1,n+1,2):
a=a+1/i
return a
print(countA(10))
sun8061
发表于 2021-1-14 08:19:56
python学习
sinaop
发表于 2021-1-14 08:28:41
看看。。。。。。。。。。。。。{:5_109:}
k客光
发表于 2021-1-23 09:55:44
def os(n):
return 1/n
def js(n):
return 1/n
s=0
l=0
n=int(input("请输入一个数:"))
if n%2==0:
for i in range(2,n+1,2):
s+=os(i)
else:
for i in range(1,n+1,2):
l+=js(i)
print(s,'\n',l)
页:
1
2
3
4
5
6
7
8
9
10
[11]
12