博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
约瑟夫问题
阅读量:3948 次
发布时间:2019-05-24

本文共 1569 字,大约阅读时间需要 5 分钟。

约瑟夫问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

n个人想玩残酷的死亡游戏,游戏规则如下:

n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。

请输出最后一个人的编号。

Input
输入n和m值。
Output
输出胜利者的编号。
Sample Input
5 3
Sample Output
4
Hint
第一轮:3被杀第二轮:1被杀第三轮:5被杀第四轮:2被杀

代码如下:

#include
#include
struct list{ int shu; struct list *next;};int main(){ struct list *head,*p,*q; int m,n,i; head=(struct list *)malloc(sizeof(struct list)); head->next=NULL; q=head; scanf("%d%d",&n,&m); for(i=1; i<=n-1; i++) { p=(struct list*)malloc(sizeof(struct list)); p->shu=i+1; p->next=NULL; q->next=p; q=p; } q->next=head; head->shu=1; p=head; n--; while(n--) { for(i=0; i
next; p->next=p->next->next; p=p->next ; } printf("%d",p->shu); return 0;}

相隔一个月再次回头做这道题,思路大体一样;上新代码:

#include 
#include
struct node{ int data; struct node*next;};int main(){ int i,n,m,j; struct node *head,*p,*tail; head=(struct node*)malloc(sizeof(struct node)); head->next=NULL; tail=head; scanf("%d%d",&n,&m); for(i=1; i<=n; i++) { p=(struct node*)malloc(sizeof(struct node)); p->data=i; tail->next=p; p->next=NULL; tail=p; }//进行编号 tail->next=head->next; p=head->next;//让单链表环化 for(i=1; i<=n; i++)//删N个人 { for(j=1; j<=m-2; j++) { p=p->next; } p->next=p->next->next; p=p->next; } printf("%d",p->data); return 0;}

转载地址:http://kzhwi.baihongyu.com/

你可能感兴趣的文章
Planning Screens and Their Relationships 规划屏幕和它们的关系
查看>>
Planning for Multiple Touchscreen Sizes 规划多个触摸屏尺寸
查看>>
Providing Descendant and Lateral Navigation 提供下一代和横向导航
查看>>
GPS 0183协议GGA、GLL、GSA、GSV、RMC、VTG解释 + 数据解析
查看>>
android如何使得电阻屏在第一次开机时自动叫起屏幕校准程序
查看>>
android如何实现:当开启图案解锁时,取消滑动解锁
查看>>
Providing Ancestral and Temporal Navigation 设计高效的应用导航
查看>>
Putting it All Together: Wireframing the Example App 把APP例子用线框图圈起来
查看>>
Implementing Lateral Navigation 实现横向导航
查看>>
Implementing Ancestral Navigation 实现原始导航
查看>>
Implementing Temporal Navigation 实现时间导航
查看>>
Responding to Touch Events 响应触摸事件
查看>>
Defining and Launching the Query 定义和启动查询
查看>>
Handling the Results 处理结果
查看>>
如何内置iperf到手机中
查看>>
如何adb shell进入ctia模式
查看>>
Contacts Provider 联系人存储
查看>>
android 图库播放幻灯片时灭屏再亮屏显示keyguard
查看>>
android 图库语言更新
查看>>
android camera拍照/录像后查看图片/视频并删除所有内容后自动回到camera预览界面
查看>>