使用C#循环链表解决约瑟夫环的问题

前几日看到一个关于约瑟夫环的面试题,于是试着动手做了一下,没有采用.Net类库的LinkedList类,自己实现了一个循环链表结构,然后模拟游戏过程得出结果。

具体代码如下:

ExpandedBlockStart.gif 代码
class  Program
    {
        
static   void  Main( string [] args)
        {
            
const   int  HUMAN_MAX  =   17 ;
            
const   int  HUMAN_INDEX  =   3 ;
            
const   int  HUMAN_NUMBER  =   0 ;
 
            LinkTable
< Human >  list  =   new  LinkTable < Human > ();
            
for  ( int  i  =   0 ; i  <  HUMAN_MAX; i ++ )
            {
                list.Add(
new  Human(i));
            }
 
            Human human 
=  list.First;
            
int  number  =  HUMAN_NUMBER;
            
do
            {
                
if  (number  ==  HUMAN_INDEX)
                {
                    number 
=   0 ;
                    Console.WriteLine(human.ID);
                    list.Remove(human);
                }
                
else
                {
                    number
++ ;
                    human 
=  (Human)human.Next;
                }
            }
            
while  (human.Next  !=  human);
            Console.WriteLine(human.ID);
 
            Console.Read();
        }
 
        
public   class  Human : LinkTableNode {
            
public  Human( int  id){
                
this .ID  =  id; 
            }
            
public   int  ID{ get ; set ;}
        }
 
        
///   <summary>
        
///  圆环链表
        
///   </summary>
        
///   <typeparam name="T"></typeparam>
         public   class  LinkTable < T >   where  T : LinkTableNode
        {
            
public  List < T >  list  =   new  List < T > ();
 
            
public  T First
            {
                
get
                {
                    
return  list.Count  >   0   ?  list[ 0 ] :  default (T);
                }
            }
 
            
public   void  Add(T t)
            {
                t.Previous 
=  list.Count  ==   0   ?  t : list[list.Count  -   1 ];
                t.Next 
=  list.Count  ==   0   ?  t : list[ 0 ];
                
if  (list.Count  >   0 )
                {
                    list[
0 ].Previous  =  t;
                    t.Previous.Next 
=  t;
                }
                list.Add(t);
            }
 
            
public   void  Remove(T t)
            {
                
int  i  =  list.IndexOf(t);
                
if (list.Count  ==   1 )
                {
                    list.Remove(t);
                }
                
else
                {
                    t.Previous.Next 
=  t.Next;
                    t.Next.Previous 
=  t.Previous;
                }
            }
        }
 
        
///   <summary>
        
///  链表节点
        
///   </summary>
         public   class  LinkTableNode
        {
            
///   <summary>
            
///  下一个节点
            
///   </summary>
             public  LinkTableNode Next
            {
                
get ;
                
set ;
            }
 
            
///   <summary>
            
///  上一个节点
            
///   </summary>
             public  LinkTableNode Previous
            {
                
get ;
                
set ;
            }
        }
    }

 

 

转载于:https://www.cnblogs.com/ruixing123/archive/2010/05/19/1739548.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
约瑟夫环问题是一个经典的数学问题,具体描述如下:有n个人(编号为1~n),围成一个圆圈,从第一个人开始报数,报到m的人出圈,然后从他的下一个人开始重新报数,重复这个过程,直到剩下最后一个人。 下面是使用循环链表解决约瑟夫环问题的C语言实现: ``` #include <stdio.h> #include <stdlib.h> typedef struct node { int num; struct node *next; } Node; void create_circle(Node **head, int n) { Node *tail = NULL, *p = NULL; for (int i = 1; i <= n; i++) { p = (Node *)malloc(sizeof(Node)); p->num = i; if (*head == NULL) { *head = p; tail = p; } else { tail->next = p; tail = p; } } tail->next = *head; // 将尾节点指向头节点,形成循环链表 } void josephus_circle(Node **head, int m) { Node *p = *head, *q = NULL; int count = 0; while (p->next != p) { count++; if (count == m) { q->next = p->next; printf("%d出圈\n", p->num); free(p); p = q->next; count = 0; } else { q = p; p = p->next; } } printf("剩下%d号\n", p->num); *head = p; } int main() { int n, m; Node *head = NULL; printf("请输入总人数n和报数m:"); scanf("%d%d", &n, &m); create_circle(&head, n); josephus_circle(&head, m); return 0; } ``` 首先,使用 `create_circle` 函数创建一个有 n 个节点的循环链表,其中头节点指针保存在 `head` 变量中。 然后,使用 `josephus_circle` 函数来模拟约瑟夫环问题使用两个指针 `p` 和 `q` 遍历循环链表, `count` 记录报数的次数。当 `count` 等于 `m` 时,删除节点 `p`,并输出其编号。否则,指针 `p` 和 `q` 向后移动。当循环链表中只剩下一个节点时,输出其编号并退出循环。 最后,将头节点指针 `head` 指向最后剩下的节点,返回其编号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值