a = 1541
b=1575
c =1512
# 我想將這些數字的總和與128的比率相比較round(a*128/total) # 等于43。
rounded_b = round(b*128/total) # equals 44
rounded_c = round(c*128/total) #等于42。
total_of_rounded = rounded_a rounded_b rounded_c #等于129而不是128。
# I tried the floor[/span]。
floor_a = math.floor(a*128/total) # equals 42
floor_b = math.floor(b*128/total) #等于43。
floor_c = math.floor(c*128/total) # 等于41。
total_of_floor = floor_a floor_b floor_c # equals 126 NOT 128
# The exact values
# a: 42.62057044[/span>
# b: 43.56093345;/span>
# c: 41,81849611[/span>
問題是,我如何才能達到總數128?
注意,我應該達到總數128。
注意:我應該停留在整數,而不是浮點數。 注2:我可以寫一個修正函式,就像把 1加到總數上一樣,但對我來說似乎并不正確。
uj5u.com熱心網友回復:
一種可能性:將a和b四舍五入,然后將缺少的部分添加到c。
a = 1541。
b=1575
c=1512
總計 = a b c # 4628
ra = a * 128 // total
rb = b * 128 // total
rc = (c * 128 (a * 128)%total (b*128)%total) // Total
print(ra,rb,rc)
# (42, 43, 43)。
print(ra rb rc)
# 128
uj5u.com熱心網友回復:
這是它的方法:
這是它的方法。
a = 1541。
b=1575
c =1512
# 我想將這些數字的總和與128的比率相比較round((a*128/total) (b*128/total) (c*128/total)
print (total_of_round)
結果。
128
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319987.html
標籤:
