鱼C论坛

 找回密码
 立即注册
查看: 2662|回复: 2

[技术交流] 关于约瑟夫问题代码的分享

[复制链接]
发表于 2020-2-27 18:12:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ByTime 于 2020-2-27 18:22 编辑

用VScode写的



  1. [code]//用循环链表模拟约瑟夫问题

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

  4. typedef struct people
  5. {
  6.     int number;
  7.     struct people *next;
  8. }people;

  9. int globoal;
  10. //循环链表的创立    创建number个节点  返回头部链表指针
  11. people *creat_struct(int number)
  12. {
  13.    
  14.     int creat_people;
  15.     int struct_size;
  16.         people *head,*p1,*p2;
  17.         globoal = number;
  18.     struct_size = sizeof(people);
  19.     head = p1 = (people*)malloc(struct_size);
  20.     //printf("请输入:");
  21.     //scanf("%d",&head->number);
  22.     head->number = 1;
  23.     for(creat_people=1;creat_people<number;creat_people++)
  24.     {
  25.         p2 = (people*)malloc(struct_size);
  26.         p1->next = p2;
  27.         p1 = p2;
  28.         //fflush(stdin);
  29.         //printf("请输入:");
  30.         //scanf("%d",&head->number);
  31.         p1->number = creat_people+1;
  32.         p1->next = head;
  33.     }
  34.     //printf("head=%p,p1=%p,p2=%p\n",head,p1,p2->next);
  35.     return head;
  36. }



  37. //约瑟夫环问题
  38. //head为传入循环链表的头结点,
  39. //number为约瑟夫问题中的数到多少要杀掉的人数;
  40. void joseph_problem(people *head,int number)
  41. {
  42.     people *del;
  43.     int first_move = number-2;
  44.     int after_move = number-1;
  45.     int read;
  46.         int move = 1;
  47.     for(;move<=first_move;move++)
  48.     {
  49.         head = head->next;
  50.     }
  51.     del = head->next;
  52.     head->next = head->next->next;
  53.     printf("->%d",del->number);
  54.     free(del);
  55.     globoal--;
  56.     while (globoal > after_move)
  57.     {
  58.         head = head->next->next;
  59.         del = head->next;
  60.         head->next = head->next->next;
  61.         printf("->%d",del->number);
  62.         free(del);
  63.         globoal--;
  64.     }
  65.     read = globoal;
  66.     while(read!=0)
  67.     {
  68.         printf("\n剩下:%d号\n",head->number);
  69.         head=head->next;
  70.         read--;
  71.     }
  72. }

  73. int main()
  74. {
  75.     int pwd;
  76.     people *headl;
  77.    //41为约瑟夫问题中代表的人的数量
  78.     head = creat_struct(41);
  79.    //3代表每次数到多少杀掉的人
  80.     joseph_problem(head,3);

  81.     return 0;
  82. }
复制代码
[/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-27 18:12:56 | 显示全部楼层
运行结果:
->3->6->9->12->15->18->21->24->27->30->33->36->39->1->5->10->14->19->23->28->32->37->41->7->13->20->26->34->40->8->17->29->38->11->25->2->22->4->35
剩下:31号
剩下:16号
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-7 10:10:04 | 显示全部楼层
虽然实现了,但是过于复杂。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 08:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表