约瑟夫环问题

#include <stdafx.h>
#include <stdlib.h>

struct number
{
 int num;
 struct number * next;
};

void main ()
{
 int m, n;
 struct number * p, * head=NULL, * tail;

 printf("please input M and N:\n");
 scanf("%d %d", &m, &n);   //输入M、N值。

 for (int i=1; i<=n; i++)               //建立循环链表。
 {
  p=(struct number *)malloc(sizeof(struct number));
  p->num=i;
  if(head==NULL){
   head=p;
                        tail=p;//注意开始tail也要赋值
               }
  else
   tail->next=p;
  tail=p;
 }
 tail->next=head;
 p = tail;  //从head开始,记录开始的前一个指针
 while(n--)   //剩下的数的个数为n     
 {       int t = m%n; //防止多数太多圈造成时间浪费                                 
  for(int j=1; j<t;j++ ) //数到要删的那个数的前一个   
          p=p->next; 
                number *q = p->next;  //要删的数的指针                  
  printf("%d ", q->num);     //输出要删的数         
  p->next = q->next;   //要删的数从链表中去掉
                free(q);          
 }
 printf("\n");

}

转载:http://apps.hi.baidu.com/share/detail/13574526

用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出 zt

题目是这样:用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至最后一个元素并输出该元素的值。写出C程序。

其实现代码如下(在READHAT9.0 LINUX下测试通过):

/********************************************
*文件名:game_ouputnum.h
*文件描述:给定M,N值,从1至N开始顺序循环数数,每数到M,把该M去掉,继续直到最后一个元数并输出该无数
         使用循环链表实现.
*作 者:szxf
*版本号:1.0.0
*版 权:
*修改记录:
*********************************************/

#include <stdio.h>
#include <stdlib.h>

/*   N 值     */
#define NUM 8   
#define OK 1       
#define ERROR -1
/* M 值 */ 
#define NEXT 3      

typedef struct
{
    int data;
    struct list *next;
}list;

/******************************************************
*函数名:list *createList()
*参 数:无
*功能描述:创建一个循环链表
*返回值:成功返回链表的首指针,失败返回-1
*抛出异常:
*作 者:szxf
******************************************************/

list *createList()
{
     list *head=NULL;
     list *tail=NULL,*temp=NULL;
     int i=0;
     head=(struct list *)malloc(sizeof(list));
if( head == NULL )
{
     printf("malloc space failed!\n");
   return ERROR;
}
head->data=1;
head->next=head;
         tail = head;
for( i=2;i<=NUM; i++)
{
    temp=(struct list *)malloc(sizeof(list));
    if( temp == NULL )
    {
      printf("malloc space failed!\n");
   return ERROR;
    }
    temp->data=i;
    temp->next=head;
    tail->next = temp;
    tail = temp;
}
return head;

}

/******************************************************
*函数名:void printList(list *head)
*参 数:链表的首地址
*功能描述:打印出链表中所有的数据
*返回值:无
*抛出异常:无
*作 者:szxf
******************************************************/

void printList(list *head)
{
    list *pList=NULL;
    pList = head;
    if( head == NULL)
{
    printf( "printList param invalid!\n" );
}
while(head->next!=pList)
{
    printf("this data is :%d\n", head->data);
    head=head->next;
}
        printf("the data is :%d\n",head->data);
return;
}

/******************************************************
*函数名:int getLastElem(list *pList)
*参 数:链表的首地址
*功能描述:开始顺序循环数数,得到最后一个元素的值.
*返回值:正确返回最后一个元素的值,错误返回-1
*抛出异常:无
*作 者:无
******************************************************/

int getLastElem(list *pList)
{
list *head=NULL,*temp=NULL;
int i = 1;
if(pList == NULL)
{
     printf("getLastElem param invalid\n");
   return ERROR;
}
head=temp=pList;
while(head!=head->next)
{
      if(i==NEXT)
   {
       temp=head;
    head=head->next;
             free(temp);
             pList->next=head;
    temp=NULL;
    i=1;
   }  
   else
   {
             pList=head;
       head=head->next;
    i++;
   }
  


}
    printf("get lastElem:%d\n", head->data);
return head->data;

}


int main()
{
    list *pList=NULL;
   
    pList=createList();
if(pList==NULL)
{
   printf("create list error!\n");
   exit(0);
}
printList(pList);
    printf("the last elem:%d\n",getLastElem(pList));

}

 

 

 

 

转载:http://blog.csdn.net/gxtdjh/article/details/6640480

约瑟夫环:用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至最后一个元素并输出该元素的值

约瑟夫环主要是要定义一个循环链表,自己写的,经过验证。  
  1.         typedef struct node  
  2. {  
  3.     int num;  
  4.     struct node *next;  
  5. }Node; //定义一个链表结构体   
  6. int main()   
  7. {  
  8.     int t =0;  
  9.                  int n=20,m=3;  
  10.     Node *head=NULL;  
  11.     Node *wei = NULL;  
  12.     //malloc后需要free掉,否则当程序   
  13.     //长时间运行时循环malloc的情况下会崩溃,   
  14.     //内存泄露,程序关闭时   
  15.     //操作系统会自动释放掉开辟内存,这里测试就不释放   
  16.     head = (Node *)malloc(sizeof(Node));  
  17.     head->num=1;  
  18.     head->next = NULL;  
  19.     wei = head;  
  20.     for(int i=2;i<=n;i++)  
  21.     {  
  22.         Node *temp = (Node *)malloc(sizeof(Node));  
  23.         temp->num = i;  
  24.         temp->next = NULL;  
  25.         wei->next = temp;  
  26.         wei = temp;  
  27.     }  
  28.     wei->next= head;   //定义一个循环链表   
  29.   
  30.      //输出合适的值   
  31.     Node *head1= wei;  
  32.     while(t<20)  
  33.     {  
  34.         int x= 0;  
  35.         while(x!=m)  
  36.         {  
  37.         head1=head1->next;  
  38.         x++;  
  39.         }  
  40.         printf("  %d",(head1)->num);  
  41.         t++;  
  42.     }  
  43.     getchar();  
  44. }  


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值