douli4337 2018-06-09 14:57
浏览 34
已采纳

php a和b玩家连续结石

I have to find a php code to solve a math problem.

This is the problem description:

Players A and B are playing a new game of stones. There are N stones placed on the ground, forming a sequence. The stones are labeled from 1 to N. Players A and B play in turns take exactly two consecutive stones on the ground until there are no consecutive stones on the ground. That is, each player can take stone i and stone i+1, where 1≤i≤N−1. If the number of stone left is odd, A wins. Otherwise, B wins. Assume both A and B play optimally and A plays first, do you know who the winner is?

The line has N stones and are indexed from 1 to N --> N (1 ≤ N ≤ 10 000 000)

If the number of stone left is odd, A wins. Otherwise, B wins.

This is my code. It does work, but it is not correct.

        <?php
$nStones =  rand(1, 10000000);
$string = ("i");
$start = rand(1, 10000000);
$length = 2;
while($nStones > 0) {
    substr( $nStones , $start [,  $length ]): string;
}
if ($nStones % 2 == 1) {
    echo "A";
} else {
echo "B";
}
?>

I think am missing the alternant subtraction of two consecutive stones by A & B, while $nStones > 0. Furthermore, the problem description mentions an optima subtraction until there is only one stone left. Therefore I guess the stones move together to their closest stones (the gaps disappear and are replaced by the closest stones).

  • 写回答

1条回答 默认 最新

  • douwenpin0428 2018-06-09 16:22
    关注

    I've made a start here:

    <?php
    class GameOfStones
    {
        const STONE_PAIR = 'OO';
        const GAP_PAIR   = '__';
    
        public $line;
    
        public function __construct($length)
        {
            $this->line = str_pad('', $length, self::STONE_PAIR);
        }
    
        // Removes a pair of stones from the line at nth location.
        public function remove($n)
        {
            if(substr($this->line, $n-1, 2) == self::STONE_PAIR)
                $this->line =
                    substr_replace($this->line, self::GAP_PAIR , $n-1, 2);
            else
                throw new Exception('Invalid move.');
        }
    
        // Check if there are no further possible moves.
        public function is_finished()
        {
            return strpos($this->line, self::STONE_PAIR) === false;
        }
    
        // Representation of line.
        public function __toString()
        {
            return implode('.', str_split($this->line)) ."
    ";
        }
    };
    
    $game = new GameOfStones(6);
    echo $game;
    var_dump($game->is_finished());
    $game->remove(5);
    echo $game;
    var_dump($game->is_finished());
    $game->remove(2);
    echo $game;
    var_dump($game->is_finished());
    

    Output:

    O.O.O.O.O.O
    bool(false)
    O.O.O.O._._
    bool(false)
    O._._.O._._
    bool(true)
    

    Currently this class starts by making a line which is a string of 'O' characters.

    So if the length was 5, the line would be a string like this:

    OOOOO
    

    The remove method takes an index. If that index was 1, first the line is checked at the string's 0 index (your n-1) for two consecutive O's. In other words 'are there stones to remove at a given position?'. If there are stones, we do a string replacement at that position, and swap the two Os for two _s.

    The is_finished method checks the line for the first occurance of two Os. In other words if there are two consecutive stones there is still a move on the line to play.

    The magic method __toString, is the string representation of a GameOfStones object. That's used as a way to visualise the state of the game.

    O.O.O.O._._
    

    The above shows four stones and two gaps (I'm not sure if the dot separators are necessary - the underscores can bleed into each other that's why I've used them).

    I have added example use of the code, where (two) pairs of stones are removed from a line of six stones. After each removal we check if there is another possible move, or rather if the game has ended.

    There is no player attribution currently, that's left to you.

    Your last rule:

    'If the number of stone left is odd, A wins. Otherwise, B wins.'

    I am struggling with. See these examples:

    i) Line of length 3:
    
    OOO 
    O__ A (1)
    
    End: one (odd) stone left.
    
    ii) Line of length 4:
    
    OOOO
    OO__ A (3)
    ____ B (1)
    
    End: zero (even) stones left.
    
    ii) Line of length 7:
    
    OOOOOOO
    O__OOOO A(1)
    O__O__O B(5)
    
    End: three (odd) stones left.
    

    I'd say that the person that removes the pair so the next player can't go is the winner. In game ii) above if A had played at position 1 (O__O), then they would prevent B from playing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥60 SOL语句中Where查询中的 from to 语句能不能从小到大换成从大到小(标签-SQL)
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))