hdu 1443

Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1303    Accepted Submission(s): 799


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
 

代码:

#include<stdio.h>

#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define len sizeof(struct line)
typedef struct line //构成链表的结点
{
int data;
struct line *next;
}list;
int visit[30]; //用于标记坏人是否被杀
list*creat(int k) //建立循环链表
{
int n;
list*p1,*p2,*head=NULL;
for(n=1; n<=2*k; n++)
{
p1=(list *)malloc(len);
p1->data=n;
if(n==1)
head=p2=p1;
else
p2->next=p1;
p2=p1;
}
p2->next=head;
return head;
}
int countnum(int k,int m,list*head)
{
int count=0,num,kill; //count用于统计被杀的人的数量,num作为每个周期的计数器,kill作为每次杀人之后,将剩下的人重新编号后的将要被杀的那个人的编号
list *p=head;
memset(visit,0,sizeof(visit)); //每次都要初始化
while(count<k) //当被杀的人的数量==k的时候,恰好所有的坏人被杀,所有的好人都活着
{
num=1; //计数时每次都要从1开始
kill=m%(2*k-count); //计算每次杀人之后,将剩下的人重新编号后,将要被杀的那个人的编号
if(kill==0) //如果剩下的人的数量恰好能被m整除,说明此时m>=剩下的人的数量了,那么将要被杀的人的编号就是最后一人
kill=2*k-count;
while(visit[p->data]!=0) //用于跳过曾经被杀的人,不计数在内
{
p=p->next;
}
while(num<kill) //找到将要被杀的那个人
{
p=p->next;
if(visit[p->data]==0)
num++;
}
if(p->data<=k&&p->data>=1) //如果将要被杀的人的是好人,则失败,说明此周期k不合适
break;
if(p->data>k&&p->data<=2*k) //如果如果将要被杀的人的是坏人,则将将要被杀的人标记为被杀,被杀的人的数量加1
{
visit[p->data]=1;
count++;
}
p=p->next; //到下一个结点
}
return count; //返回被杀的人的数量
}

int main()
{
int n,i,k,j,a[16],m,t;
list *p1,*p2,*head;
for(k=1;k<14;k++) //打表,避免超时
{
head=creat(k); //建立链表
if(k<=9) //当k<=9的时候,周期j并没有超过2000
{
for(j=k+1;;j++)
{
t=countnum(k,j,head);
if(t==k)
break;
}
}
else
{
for(j=90000;;j++) //当k>9的时候,周期j已经超过了90000,所以从90000开始,避免超时
{
t=countnum(k,j,head);
if(t==k)
break;
}
}
a[k]=j; //一旦找到合适的周期,将其存入数组中
}
while(~scanf("%d",&k)&&k)
{
printf("%d\n",a[k]);
}
}

转载于:https://www.cnblogs.com/lxm940130740/p/hdu1443.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值