0

基本上,这与我们有 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

4 回答 4

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;

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

于 2012-10-17T05:08:32.757 回答
0

从获胜者的角度来看,所有大分都在左边,否则在右边。所以你可以检测到第一个分数。

$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;
}
于 2012-10-17T05:01:12.827 回答
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;
}
于 2012-10-17T06:00:57.440 回答
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]." ";
    }

}

?>
于 2012-10-17T06:14:05.293 回答