Problem Description
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

 
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

 
Output
The output file will consist of separate lines containing m corresponding to k in the input file.

 
Sample Input
3
4
0
 
Sample Output
5
30
题意:给出一个k,代表有K个好人与K个坏人,其中前K个是好人,后K个是坏人,根据约瑟夫环游戏的原理,在坏人都出局而好人没有一个都没有出局的情况下,m最小是多大


#include<stdio.h>
#include<malloc.h>
#include<string.h>
int vist[30];
typedef struct list
{
int data;
struct list *next;
}Lnode,*LinList; void creat_L(LinList *L)
{
(*L)=(LinList)malloc(sizeof(Lnode));
(*L)->data=0;
(*L)->next=(*L);//形成环
}
int DeleteNode(LinList L,int k,int n)//n是走的步数,k是有多少个好人和坏人
{
int j,i=0,badk=0,mod;
LinList q=L; while(i<k)//i是代表有i个坏人被处决
{
q=q->next;
j=1;//j=1是从当前位置开始计数,看下面的代码
while(vist[q->data]||q->data==0)//跳过己经处决的,vist为0时,表示这人还活着
{
q=q->next;
}
mod=n%(2*k-i);//(2*k-i)代表活着的人数,mod就等于走了很多圈以后余下的步数少于活着的人数
if(mod==0)//代表从当前位置走了n步后又最后会回到当前位置,那么就等于活着的人数
mod=2*k-i;
while (j<mod)//开始走
{
q=q->next;
if(vist[q->data]==0&&q->data!=0)//为了跳过处决了的,活着才能+1,算是一步
j++;
}
if(q->data<=k)//代表处决的是好人,不满足只处决坏人,不用往下走了,跳出循环
break;
if(q->data>k)//大于k的说明处决的是坏人,那么计数加一个
badk++;
vist[q->data]=1;//代表处决了
i++; //下一次 //printf("%d ",q->data);
}
return badk;//反回处决坏人的人数
} void Crea_list(LinList L,int n)//创建一个以n个人的圈
{
int i;
LinList q=L,p;
for(i=n;i>=1;i--)
{
p=(LinList)malloc(sizeof(Lnode));
p->data=i;
p->next=q->next;
q->next=p;
}
} int main()
{
int i,k,m,j,t,a[16];
LinList L,q;
for(j=1;j<14;j++)//打表
{
creat_L(&L);
Crea_list(L,2*j);
q=L;
if(j<=9)
for(m=j+1;;m++)//表法要直的步数
{
memset(vist,0,sizeof(vist));
t=DeleteNode(L,j,m); if(t==j)//表法以当前的步数走,处决的坏个等于坏人总数,就跳出来
break;
}
else
{
m=90000;
for(;;m++)
{
memset(vist,0,sizeof(vist));
t=DeleteNode(L,j,m); if(t==j)
break;
}
}
a[j]=m;
}
while(scanf("%d",&k)>0&&k)
{
printf("%d\n",a[k]);
}
}
/*
1
2
2
7
3
5
4
30
5
169
6
441
7
1872
8
7632
9
1740
10
93313
11
459901
*/

 

hdu1443(约瑟夫环游戏的原理 用链表过的)的更多相关文章

  1. 简单约瑟夫环的循环单链表实现(C++)

    刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...

  2. 3,java数据结构和算法:约瑟夫环出队顺序, 单向环形链表的应用

    什么是约瑟夫环? 就是数小孩游戏: 直接上代码: 要实现这个,只需要理清思路就好了 孩子节点: class Boy{ int no;//当前孩子的编码 Boy next; // 下一节点 public ...

  3. 约瑟夫环问题 --链表 C语言

    总共有m个人在圆桌上,依次报名,数到第n个数的人退出圆桌,下一个由退出人下一个开始继续报名,循环直到最后一个停止将编号输出 #include <stdio.h>#include <s ...

  4. 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)

    约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...

  5. 约瑟夫环问题的链表解法和数学解法(PHP)

    约瑟夫环问题 一群猴子排成一圈.按1,2,-,n依次编号.然后从第1仅仅開始数,数到第m仅仅,把它踢出圈.从它后面再開始数,再数到第m仅仅.在把它踢出去-.如此不停的进行下去.直到最后仅仅剩下一仅仅猴 ...

  6. Java数据结构之单向环形链表(解决Josephu约瑟夫环问题)

    1.Josephu(约瑟夫.约瑟夫环)问题: 设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m ...

  7. 单向环形链表解决约瑟夫环(Josephus)问题

    一.约瑟夫环问题 Josephu 问题为:设编号为1,2,- n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那 ...

  8. C语言链表实现约瑟夫环问题

    需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...

  9. C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析

    尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...

随机推荐

  1. Windows8.1使用博客客户端写博客

    1.首先去微软官网下载客户端(Windows live writer) http://windows.microsoft.com/zh-cn/windows-live/essentials 安装步骤 ...

  2. Android studio快捷键Windows版本

    为了方便大家记住这些小技巧和快捷键,我把它写成了一个插件,欢迎大家下载使用:http://chunsheng.me/EasyShortcut/ 快捷键 描述 通用------------------- ...

  3. php中对象的串行化

    我们大家有知道PHP串行化可以把变量包括对象,转化成连续bytes数据,你可以将串行化后的变量存在一个文件里或在网络上传输,然后再反串行化还原为原来的数据.文章这里就PHP串行化为大家详细的介绍.你在 ...

  4. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

  5. s3c2416裸跑环境配置

    最近刚刚开始学习ARM-linux,上周买了块tq2416的板子,给的Linux资料太复杂太深奥不愿看,等不及想要把2416跑起来.于是到处找相关裸跑资料,可是用2416的人实在少,网上的资料更少,裸 ...

  6. 在VS里面查看lua堆栈

     extern std::string get_lua_stack(void); std::string stack = get_lua_stack();    std::string get_lua ...

  7. 《Programming WPF》翻译 第8章 1.动画基础

    原文:<Programming WPF>翻译 第8章 1.动画基础 动画包括在一段时间内改变用户界面的某些可见的特征,如它的大小.位置或颜色.你可以做到这一点,非常困难的通过创建一个tim ...

  8. How to find friends

    How to find friends 思路简单,编码不易 1 def check_connection(network, first, second): 2 link_dictionary = di ...

  9. centos下安装mysql步骤

    转载自http://zym820910.iteye.com/blog/671566 一.下载MySQL5.0和相关perl包 1.官网[url] http://dev.mysql.com/downlo ...

  10. 【转】gcc warning: braces around scalar initializer (标量初始化的括号)

    原文网址:http://stackoverflow.com/questions/3462513/gcc-warning-braces-around-scalar-initializer I have ...