0

番号があるとします。たとえば 5 とします。ここで、メンバーが 5 人いるとします。ここで、各メンバーは 1 対 2 のカウントを開始します。2 番目の番号を取得したメンバーは退席し、次のメンバーから再びカウントを開始します。したがって、このシナリオでは、3 番目のメンバーが最後に残ります。

だから私はこのように実装しようとしました。最初にメンバーを配列 $v として割り当てます。

for($i=1 ; $i<=5 ; $i++)
{
    $v[] = $i;
}

$v1 = array_flip($v);

for($i=0 ; $i<=5 ; $i += 2 )
{   
    unset($v1[$i]);
}

echo "<pre>";
print_r($v1);

出力

Array
(
    [1] => 0
    [3] => 2
    [5] => 4
)

ここで、キー 5 (5 番目のメンバー) から 1 (1 番目のメンバー) までの数字を数えたいと思います。

とうとうキー3(3rdメンバー)が抜けました。
最後に去ったメンバーを印刷したい。

どうすればこれを達成できますか?

私はあなたが理解できないなら、この サバイバル戦略を見てください

4

5 に答える 5

1

これにより、残りの項目が 1 つになるまで、他のすべての項目が配列から削除されます。

$members = range(1, 5);

$i = 0;

while(count($members) > 1) {
 $i++;
 if($i == count($members)) $i = 0;
 unset($members[$i]);
 $members = array_values($members);
 if($i == count($members)) $i = 0;
}

echo $members[0];
于 2013-03-22T15:13:26.360 に答える
1

これは、わかりやすい reduce メソッドと複数の例を使用したオブジェクト指向ソリューションです。

class CountByTwoArrayReducer {

  public function __construct($array) {
    $this->array = $array;
    $this->size  = count($array);
  }

  public function reduce() {
    $this->initialize();

    while($this->hasMultipleItems()) {
      $this->next();
      $this->removeCurrentItem();
      $this->next();
    }

    return $this->finalItem();
  }

  protected function initialize() {
    $this->current   = 1;
    $this->removed   = array();
    $this->remaining = $this->size;
  }

  protected function hasMultipleItems() {
    return ($this->remaining > 1);
  }

  protected function next($start = null) {
    $next = ($start === null) ? $this->current : $start;

    do {
      $next++;
    } while(isset($this->removed[$next]));

    if($next > $this->size)
      $this->next(0);
    else
      $this->current = $next;
  }

  protected function removeCurrentItem() {
    $this->removed[$this->current] = 1;
    $this->remaining--;
  }

  protected function finalItem() {
    return $this->array[$this->current - 1];
  }

}

$examples = array(
  array('A', 'B', 'C', 'D', 'E'),
  range(1, 100),
  range(1, 1000),
  range(1, 10000)
);

foreach($examples as $example) {
  $start = microtime(true);

  $reducer = new CountByTwoArrayReducer($example);
  $result  = $reducer->reduce();

  $time = microtime(true) - $start;

  echo "Found {$result} in {$time} seconds.\n";
}
于 2013-03-25T20:01:10.077 に答える
0

この答えはもう少し複雑ですが、はるかに効率的です。アイテムの配列を作成してから削除するわけではありません。値 (例: 1) で始まり、まだ削除されていない次の項目を計算します。次に、削除済みとしてフラグを立てます。実際に項目の配列がある場合、最終的な項目のインデックスは $current - 1 になります。以下の例では、1 ~ 10,000 の値を使用しています。私のマシンでは、0.05 秒強かかります。

define('SIZE', 10000);

/**
 * Helper function to return the next value
 */
function get_next($current, &$removed) {
  $next = $current;
  do {
    $next++;
  } while(isset($removed[$next]));

  return ($next > SIZE) ? get_next(0, $removed) : $next;
}

$current   = 1;
$removed   = array();
$remaining = SIZE;

$start_time = microtime(true);

while($remaining > 1) {
  $current           = get_next($current, $removed);
  $removed[$current] = 1;
  $remaining         = SIZE - count($removed);
  $current           = get_next($current, $removed);
}

$total_time = microtime(true) - $start_time;

echo "Processed " . SIZE . " items\n";
echo "Winning item: {$current}\n";
echo "Total time: {$total_time} seconds\n";
于 2013-03-25T18:53:04.253 に答える
0
<?php

function build_thieves($thieves)
{
    return range(1, $thieves);
}

function kill_thief(&$cave)
{
    if(sizeof($cave)==1)
    {
        $thief=array_slice($cave, 0, 1);
        echo $thief.' survived';
        return false;
    }

    $thief=array_slice($cave, 0, 1);
    array_push($cave, $thief);

    $thief=array_slice($cave, 0, 1);
    echo $thief.' killed';
    return true;
}

$cave=build_thieves(5);
$got_data=true;
while($got_data)
{
    $got_data=kill_thief($cave);
}

3日ごとではなく2日ごとに調整。そして、0ではなく1から始まります

于 2013-03-22T15:20:49.617 に答える
0

うーん、2 つの機能をお勧めできます。

  1. http://php.net/manual/en/function.array-keys.php

    これにより、インデックス0、1、2で配列のインデックスが再作成されます

  2. http://php.net/manual/en/control-structures.foreach.php

    これにより、次の方法で任意の配列を通過できます。

    foreach($v1 as $key=>$value) { ...最大値を選択 ... }

于 2013-03-22T14:59:37.263 に答える