1樓:樹不開叉
def summa():
summ = 0;
for i in range(1,100):
fl = 1.0/i;
if fl>=0.1:
summ += fl
else:
print "the sum is",summreturn sum
summa()
(注意縮排)
2樓:
def iteritem(limit):
x = 1.
while 1:
v = 1 / x
if v < limit:
raise stopiteration
yield v
x += 1
print sum([n for n in iteritem(limit=0.1)])
3樓:西邊有片雲
def f(n):
sum=0
for x in range(1,n+1):
k=1.0/x
if k>=0.1:
sum=sum+k
else:
break
return sum
除錯結果:
print f(11)
2.92896825397
print f(10)
2.92896825397
然後你自己做下字串和數字的限制。就可以了
用python計算1/1!,1/2!,1/3!……1/n!,當某一數列的值小於0.01時停止計算?
4樓:草原上之狼
t=1mult=1
sum1=0
i=2while(t>0.1):
t=1/mult
sum1=sum1+t
mult=mult*i
i=i+1
print(sum1)
菜鳥問一下,怎麼用python程式設計 1/0!+1/1!+1/2!+.....+1/10000!
5樓:匿名使用者
你確定0可以作為分母? 你就一個迴圈計算階層累加
6樓:句心後端開發
**示例來
"""菜鳥問源
一下,怎麼用python程式設計 1/0!+1/1!+1/2!+.....+1/10000!
"""import math
result = 0
for i in range(0, 10001):
result += 1/math.factorial(i)print(result)
輸出2.7182818284590455
輸出截圖
7樓:匿名使用者
def jc(n):
if n==0:
return 1
else:
s=1for i in range(n,n+1) :
s=s*i
return s
def cj1():
s=0for i in range(10001):
s= s+1/ jc(i)
return s
cj1()
忘記map怎麼用了,想了個回方法答
python 程式設計 求答案!2、3兩題
8樓:傳聞中的張先生
#-*- coding:utf-8 -*-
import time
import datetime
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def msg():
f = open('test.txt','r')
'''李麗麗 320382199606160123
徐華彩 320382199606160134
藺小虎 320382199606160145
葛俊 320382199606160156
'''res = f.readlines()
for i in range(len(res)):
result = res[i].replace(' ', '').replace('\t', '').replace('\n', '').replace('\r', '')
name = result[0:-18]
num = result[-2:-1]
now_time = datetime.datetime.now()
time = datetime.datetime.now().strftime('%y%m%d')
year = result[-12:-4]
age = int(time[0:4]) - int(year[0:4])
if int(time[4:]) > int(year[4:]):
age = age
else:
age = age - 1
if int(num) % 2 == 0:
*** = "女".decode('utf-8').encode('gbk')
else:
*** = "男".decode('utf-8').encode('gbk')
with open('oeder.txt', 'a') as f:
f.write(str(name) + ' ' + str(***) + ' ' + str(age) + '\n')
f.close()
f.close
if __name__ == "__main__":
start = time.clock()
msg = msg()
end = time.clock()
print u'儲存完成,共耗時:'+str(end - start)
9樓:匿名使用者
這要寫多少**啊?分太少了。
怎麼用C語言程式設計求 1
include int main int argc,char argv include int main printf f n s return 0 用c語言程式設計 求1 1 2 1 3 1 4 1 5 1 6 1 99 1 100 的值 include int main printf lf n ...
C語言程式設計1用子函式求cosx1x
include include void main while fabs term 1e 5 printf cos g 5f n x,sum c語言 用子函式求cos x 1 x2 2 x4 4 x6 6 看詳情 題目所說的 當取8位小數時,結果約為1.86602621 有誤。經用windows計算...
用c語言程式設計用遞迴的方法求fn
include stdio.h int f int n void main include int main void printf f d d n n,sum return 0 c語言求次方,用遞迴的方法求 f n 1 1 2 2 3 3 n n include stdio.h include m...