php購票排位 php投票排名演算法

2021-10-13 03:47:26 字數 3784 閱讀 9984

投票排名演算法(php版),參考了reddit的排名演算法。<?php

演算法預習

abs:取絕對值

max:取最大值

log10:常用對數,log10(x) = y,隨著x的增大,y的增加越來越小

使用方法:

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

namespace common;

class votescore

function __construct($ups, $downs, $time)

$this->_ups = $ups;

$this->_downs = $downs;

$this->_time = $time;

基本問題

差值越大,得分越高

function score1()

$score = log10(max(abs($this->_ups - $this->_downs),1));

return $score;

解決爭議

總數越多,得分越高 1000:999 大於 10:9

1000:999 和 10:9 在score1裡得出的值相等,score2就是為了解決這個問題

function score2()

$sum = $this->_ups+$this->_downs;

if($sum < 1)elseif($this->_ups == $this->_downs)elseelseif($this->_ups == $this->_downs)else{

$y = -1;

if($this->_ups > 0){

$y = $y*(log10($this->_time)-9);

return $y;

public function getscore()

//預設都有一票贊成票和一票反對票

$this->_ups = $this->_ups+1;

$this->_downs = $this->_downs+1;

$score = $this->score1() + $this->score2() + $this->score3() + $this->score4();

$score = round($score, 4);

return $score;

測試資料如下:<?php

$ups = 2;

$downs = 0;

$time = time()+3600*24*2;

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 3;

$downs = 0;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 10;

$downs = 2;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 100;

$downs = 101;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 10000;

$downs = 10001;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 1000;

$downs = 100;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 1000;

$downs = 10;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 90;

$downs = 100;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 100;

$downs = 100;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 20;

$downs = 100;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 0;

$downs = 1;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 10;

$downs = 0;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

$ups = 0;

$downs = 1000;

$time = time();

$vote = new \common\votescore($ups, $downs, $time);

$score = $vote->getscore();

echo $score;

echo "\n";

php 投票 調查

投票調查 include islogin.php include conn config.inc.php action操作初始化 if empty request action else if request action add elseif request action insert if rs...

PHP 練習 投票

練習 投票 一 題目要求 二 做法 建立資料庫 表名 diaoyantimu 表名 diaoyanxuanxiang 封裝類檔案 1 2class dbda335 else 364041 42else 4347 4849 50 51 5253 5455 56 首頁 1 public w3c dtd ...

PHP投票練習

1 連線資料庫查詢資料顯示在頁面中 2 將需要改變資料的主鍵值提交到處理頁面 3 處理頁面中通過主鍵值找到資料中的計數列的值,將計數列的的值加1複製給計數列 4 在另乙個頁面顯式結果 可以複製貼上執行 資料庫表 效果圖結果圖 主頁面 include dbda.class.php db new dbd...