我知道這個問題可能比編程更多的是數學,但我正在嘗試解決它以撰寫一個簡單的 Python 程式,所以我希望我能在這里找到一些幫助。
我試圖找出候選人在棄權的情況下進入第二輪選舉需要達到什么分數。規則很簡單:如果候選人達到 12.5% 的登記選民,那么他就可以進入第二輪。
因此,如果棄權率為 50%,則任何給定的候選人都需要在投票的人中得分 25% 才能進入第二輪投票。
我可以成功地計算出如何將投票者的百分比分數轉換為已登記棄權數量的百分比分數。因此,下面的函式中,percentage_candidate 是表達投票的百分比,棄權是未投票的登記選民總數的百分比:
def calc_percentage_registered(percentage_candidate, abstention):
participation = 100-abstention
registered_score = percentage_candidate*(participation/100)
print(f'With a score of {percentage_candidate}% of the expressed voters and {abstention}% abstention, I have {registered_score}% of registered voters.')
return registered_score
該函式回傳正確的輸出。我用一些示例值運行它:
calc_percentage_registered(25,0)
calc_percentage_registered(25,25)
calc_percentage_registered(25,50)
calc_percentage_registered(25,75)
輸出是正確的:
With a score of 25% of the expressed voters and 0% abstention, I have 25.0% of registered voters.
With a score of 25% of the expressed voters and 25% abstention, I have 18.75% of registered voters.
With a score of 25% of the expressed voters and 50% abstention, I have 12.5% of registered voters.
With a score of 25% of the expressed voters and 75% abstention, I have 6.25% of registered voters.
現在我該如何計算候選人在給定棄權時需要的登記選民百分比?我知道我需要撰寫一個函式并傳遞一個引數(棄權),但我不知何故卡在這一步,感覺自己像個白癡!
非常感謝大家的幫助
uj5u.com熱心網友回復:
這是你正在尋找的方程嗎?
def calc_percentage_registered_voters_needed(abstention, threshold=0.125):
return threshold / (1-abstention)
calc_percentage_registered_voters_needed(abstention=0.6)
您需要將您使用的百分比項除以 100。
uj5u.com熱心網友回復:
假設總人口為P,A人們決定不投票。在這種情況下,投票的人數是P - A。假設您需要W選票才能獲勝。
給定數量a = A / P和w = W / P。您的目標是x = W / (P - A)用aandw而不是A, Wand來表達P。
從一些替換開始:
x = w * P / (P - a * P)
你可以分出來P得到
x = w / (1 - a)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471331.html
