优化逻辑找到高分

时间:2012-10-17 04:54:42

标签: php

基本上这与我们有2个分数的壁球应用有关。一个是从赢家的角度来看,另一个是从失败者的角度来看。

例如。 得分1:11-5,11-5,11-5(获胜者观点) 得分2:5-11,5-11,5-11(失败者观点)

现在按照我的逻辑,我想找到哪个是赢家分数,哪个是输家分数。

我用以下方式编写了我的逻辑,它确实有效。但我想知道他们是否有其他更好/更优化的写作方式。     

$high1 = 0;
$high2 = 0;

$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";

$score1Array = explode(",",$score1);

$size = sizeof($score1Array);

for($i = 0; $i < $size; $i++) {

$checkscore1 = explode("-",$score1Array[$i]);


if($checkscore1[0] < $checkscore1[1]) {

    $high1++;

}else if($checkscore1[0] > $checkscore1[1]) {

    $high2++;

}


}

if($high1 > $high2) {

$winningScore = $score2;
$losingScore = $score1;
}else{

$winningScore = $score1;
$losingScore = $score2;
}

echo $winningscore;
echo $losingscore;

4 个答案:

答案 0 :(得分:1)

这样的事情:

function is_winning($score) {

  $split_scores = preg_split('/(-|,)/', $score);
  $wins = $losses = 0;

  for($i = 0; $i < count($split_scores) / 2; $i += 2) {
    if($split_scores[$i] > $split_scores[$i + 1])
      $wins++;
    if($split_scores[$i] < $split_scores[$i + 1])
      $losses++;
  }

  return $wins > $losses;

}

假设$score格式化为您的问题。然后你可以像这样使用它:

$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";

if(is_winning($score1)) {
  $winning_score = $score1;
  $losing_score = $score2;
} else {
  $winning_score = $score2;
  $losing_score = $score1;
}

echo $winning_score;
echo $losing_score;

这个想法是将得分分成一个数组,其中偶数编号的索引具有左侧分数,奇数编号的索引具有右侧分数。然后我们计算胜利次数和损失次数。如果有更多的胜利然后是损失,那么我们返回真实,因为得分是一个获胜的分数。如果没有更多的胜利然后损失我们只会返回假。

答案 1 :(得分:0)

对于Winner来说,左边的所有大分,否则在右边。所以你可以检测到第一个分数。

$score1Array = explode(",",$score1);
$checkscore1 = explode("-",$score1Array[$i]);
if($checkscore1[0] < $checkscore1[1]) {
    echo $score2;
    echo $score1;
}else{
    echo $score1;
    echo $score2;
}

修复:上面的代码错误,试试这个:

$score1value = eval(str_replace(",","+",$score1));
$score2value = eval(str_replace(",","+",$score2));

if($score1value < $score2value) {
    echo $score2;
    echo $score1;
}else{
    echo $score1;
    echo $score2;
}

答案 2 :(得分:0)

这应该有效

$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$l = $r = 0;

$score1_sets_arr = explode(',', $score1);
foreach ($score1_sets_arr as $set_score) {
  $set_score_arr = explode('-', $set_score);
  if ($set_score_arr[0] > $set_score_arr[1]) {
    $l++;
  } else {
    $r++;
  }
}

if ($l > $r) {
  $winning_score = $score1;
  $losing_score  = $score2;
} else {
  $winning_score = $score2;
  $losing_score  = $score1;
}

答案 3 :(得分:0)

你可以用这个:

<?php
$high1 = 0;
$high2 = 0;

$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";

$explode = explode(",",$score1);


for($i=0;$i<  sizeof($explode);$i++){
    $explode2= explode("-", $explode[$i]);
    if($explode2[0] <= $explode2[1]){
        echo $explode2[0]."-";
        echo $explode2[1]." ";
    }

}
echo "<br />";
for($i=0;$i<  sizeof($explode);$i++){
    $explode2= explode("-", $explode[$i]);
    if($explode2[1] >= $explode2[0]){
        echo $explode2[1]."-";
        echo $explode2[0]." ";
    }

}

?>