编程16:环形单链表的约瑟夫问题

<?php
header("content-type:text/html;charset=utf-8");
/*
 *单链表的约瑟夫问题 P43
 */
class Node{
    public $value;
    public $next;
    public function __construct($value)
    {
        $this->value = $value;
    }
}

function josephusKill1($head,$num){
    if($head == null || $head->next == null || $num == 0){
        return $head;
    }

    $last = $head;
    while ($last->next != $head){
        $last = $last->next;
    }
    $count = 0;
    while ($last != $head){
        if(++ $count == $num){
            $last->next = $head->next;
            $count = 0;
        }
        else{
            $last = $last->next;
        }
        $head = $last->next;
    }
    return $head;
}

function josephusKill2($head,$num){
    if($head == null || $head->next == null || $num == 0){
        return $head;
    }

    $cur = $head->next;
    $tem = 1;

    while ($cur != $head){
        $tem ++;
        $cur = $cur->next;
    }
    $tem = getLive($tem,$num);
    while (-- $tem != 0){
        $head = $head->next;
    }
    $head->next = $head;
    return $head;

}

function getLive($i,$num){
    if($i == 1){
        return 1;
    }
    return (getLive($i - 1,$num) + $num - 1) % $i +1;
}

$head1 = new Node(1);
$head1->next = new Node(2);
$head1->next->next = new Node(3);
$head1->next->next->next = new Node(4);
$head1->next->next->next->next = new Node(5);
$head1->next->next->next->next->next = $head1;
echo "初始循环列表为:";
echo "</br>";
print_r($head1);
echo "</br>";
echo "</br>";
echo "方法1剩余结点为:";
print_r(josephusKill1($head1,2));

echo "</br>";
$head2 = new Node(1);
$head2->next = new Node(2);
$head2->next->next = new Node(3);
$head2->next->next->next = new Node(4);
$head2->next->next->next->next = new Node(5);
$head2->next->next->next->next->next = $head2;
echo "方法2剩余结点为:";
print_r(josephusKill2($head2,2));

输出结果:

 

转载于:https://www.cnblogs.com/xlzfdddd/p/10050331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值