題目:
1紅包演算法
2 1.輸入是人數,總金額。輸出是每個人的金額
3 2.多人紅包,最大的不得超過總額的80%
**:
1#@author :whycai2#
@time :2021/3/4 22:1534
'''5
紅包演算法,輸入是人數,總金額。輸出是每個人的紅包金額,拿的最多的人應有標示。
6額外要求:多人紅包下,拿的最多的人,不得超過總額的80%.
7'''
8import
random910
def getred(money,num,maxnum=0.8,min=0.01,switch=1):
11'''
12求隨機數
13:param money: 總金額
14:param num: 人數
15:param maxnum: 個人最大值的係數(總金額的百分比)
16:param min: 最小值
17:param switch: 1:隨機數 第乙個人獲得大的金額數概率較大 其他:最大值按照 剩餘金額/(剩餘人數/2)的方式取值
18:return:
19'''
20 money = round(float(money),2)
21 max = money *maxnum
22 num =int(num)
23if num == 1:
24print('
總人數:,紅包金額:
'.format(num=num,money=money))
2526 total =money
27if num > 1:
28for i in
range(num):
29if i != num-1:
30 ranmax = int(max*100-(num-i-1)*min*100)
31if switch !=1:
32 mid = 2 if num - i -1 < 2 else int((num - i - 1)/2)
33 ranmax = round(ranmax/mid)
34 ranred = random.randint(min*100, ranmax)
35 ranredfloat = float(ranred/100)
36 total = round((total - ranredfloat),2)
37if total 38 max =total
39else
:40 ranredfloat =total
41 total =0
42print('
總人數:,第個人的紅包金額:,剩餘紅包金額:
'.format(num=num, n=i+1,money=ranredfloat,moneyother=total))
43 getred(100,10,0.8,0.01,2)
python 練習題 檔案取值排序
題目 1 三個檔案 txt1.txt txt2.txt txt3.txt 21.取出txt1.txt中的第二列資料,32.取出txt2.txt中的第三列資料 43.排序,從大到小 自己實現 54.寫入 txt3.txt 中滴 6按照從大到小的順序,寫入3.txt中的第一列78 例 9txt1.txt...
python書中練習題 python練習題
1 定義乙個空列表,接收從鍵盤輸入的整數,把列表傳給乙個從大到小排序的函式,再輸出排序後的列表的值 listex b 0 a int input 請輸入列表長度 while b a num int input 請輸入字元 b 1 print listex sum 0 for i in range 0...
python的練習題 Python練習題
1 使用while迴圈輸入1 2 3 4 5 6 8 9 10 i 0while i 10 i i 1 if i 7 continue print i 結果 e python python python test.py1 2 求1 100的所有數的和 i 0sum 0 while i 100 i 1...