• 数据结构:约瑟夫环


    约瑟夫环

    一位叫约瑟夫的将军,战斗中,连同手下士兵一起被敌人俘虏了,

    手下的士兵宁死不投降,所以,约瑟夫将军想出了一个办法,

    让大家站成一个圈,从第一个人开始数数,谁数到7了,谁就自杀,

    自杀之后,他的下一个人重新从1开始数,遇到7,再自杀,直到最后只能剩下一个人。

    最后活下来的是约瑟夫,然后他不想死,他投降了。

    这种圈,在现今社会中被叫做,"约瑟夫环"。

    要求,编写程序,命令行 输入 总人数(>=2) 及 数到几(>=2)自杀,

    程序自动计算,将第 n 次 杀掉的 第m个人输出

    最后再输出剩下的是几号。

    例如:

    输入 5人 数到3 自杀,

    程序输出:

    第 1 次杀的人是 3 号;

    第 2 次杀的人是 1 号;

    第 3 次杀的人是 5 号;

    第 4 次杀的人是 2 号;

    最后剩下的是 4 号。

    2.代码实现

    2.1头文件

    1. #ifndef _MYHEAD_H_
    2. #define _MYHEAD_H_
    3. #include
    4. #include
    5. typedef struct _NODE{
    6. int data;
    7. struct _NODE *next;
    8. }looplist;
    9. looplist* looplistCreate();
    10. int looplist_inputdata(looplist* phead,int value);
    11. void looplistshow_info(looplist* phead);
    12. looplist* looplist_cutHead(looplist* phead);
    13. #endif

    2.2函数文件

    1. #include "myhead.h"
    2. looplist* looplistCreate()
    3. {
    4. looplist* phead;
    5. phead=(looplist*)malloc(sizeof(looplist));
    6. phead->data=0;
    7. phead->next=phead;
    8. return phead;
    9. }
    10. int looplist_inputdata(looplist* phead,int value)
    11. {
    12. if(phead==NULL){
    13. printf("传参错误\n");
    14. return -1;
    15. }
    16. looplist* temp=(looplist*)malloc(sizeof(looplist));
    17. temp->data=value;
    18. temp->next=NULL;
    19. temp->next=phead->next;
    20. phead->next=temp;
    21. return 0;
    22. }
    23. int looplist_delete(looplist* phead)
    24. {
    25. if(phead==NULL){
    26. printf("传参错误\n");
    27. return -1;
    28. }
    29. }
    30. looplist* looplist_cutHead(looplist* h)
    31. {
    32. if(h==NULL){
    33. printf("传参错误\n");
    34. exit(-1);
    35. }
    36. looplist* p=h;
    37. while(h->next!=p){
    38. h=h->next;
    39. }
    40. h->next=p->next;
    41. free(p);
    42. p=NULL;
    43. return h->next;
    44. }
    45. void looplistshow_info(looplist* phead)
    46. {
    47. if(phead==NULL){
    48. printf("传参错误\n");
    49. return ;
    50. }
    51. looplist* flag=phead;
    52. while(phead->next!=flag){
    53. printf("%d ",phead->data);
    54. phead=phead->next;
    55. }
    56. printf("%d\n",phead->data);
    57. }

    2.3main.c

    1. #include "myhead.h"
    2. void yuesefu(int n,int k,int m)
    3. {
    4. looplist* phead = looplistCreate();
    5. for (int i = n; i>=1; i--){
    6. looplist_inputdata(phead,i);
    7. }
    8. phead = looplist_cutHead(phead);
    9. for (int i = 0; i < k-1; i++){
    10. phead = phead->next;
    11. }
    12. //printf("%d\n",phead->data);
    13. looplist* temp;
    14. while (phead->next != phead){
    15. for (int i = 0; i < m-2;i++){
    16. phead = phead->next;
    17. }
    18. temp = phead->next;
    19. phead->next = temp->next;
    20. printf("%d ",temp->data);
    21. free(temp);
    22. temp = NULL;
    23. phead = phead->next;
    24. }
    25. printf("%d\n", phead->data);
    26. free(phead);
    27. phead = NULL;
    28. }
    29. int main(int argc,char const* argv[])
    30. {
    31. yuesefu(5,1,3);
    32. return 0;
    33. }

  • 相关阅读:
    刷题知识回顾《四》多数元素反转链表
    机械设计基础试题3
    SpringBoot+LayUI+MybatisPlus 前后端分离 实现排名统计功能
    SpringMVC01(入门)
    批量汇总nmon结果文件Excel数据
    WhatsApp营销引流-SendWS拓客系统功能后台介绍(五):WhatsApp素材管理
    新零售行业如何玩转线上服务
    数据分析三剑客
    豆瓣电影信息爬虫实战-2024年6月
    分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
  • 原文地址:https://blog.csdn.net/a2998658795/article/details/126375452