POJ 1012 约瑟夫问题

5 篇文章 0 订阅

Joseph
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 55108
Accepted: 21067

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

Source
Central Europe 1995

哈哈,我遇到最坑的约瑟夫问题!!!从小老师就要我们用循环链表来解约瑟夫环,然而这题算是颠覆认知了,由于数据规模非常大,k=13时,m就是2504881,意味着要建2504881个循环链表……结果就是炸炸炸……k=11时,我的电脑就花了40分钟才算出m

所以要想AC,用传说中的打表大法吧,再配合枚举
不妨将n个人编号0,1,2,3……n-1 现在数到m的killed ,第一个肯定m-1, kill以后,环里还有n-1-1 个人,所以第二个被kill 的 是 (上一个编号+m-1)%(n-1-i) i是已经被杀过的人。所以有递推公式:
ans[i]=(ans[i-1]+m-1)%(n+1-i);

以下AC代码:

#include<iostream>
using namespace std;
int main()
{
int Joseph[14]={0};//存结果,防止重复数据爆时间
int k;
while(cin>>k&&k!=0){
    if(Joseph[k]){
        cout<<Joseph[k]<<endl;
        continue;
    }
    int n=2*k;// 开始时总共有2*k个人 
    int m=k+1;
    int ans[100]={0};//代表第n 次kill 的编号 
    for(int i=1;i<=k;i++)
    {
        ans[i]=(ans[i-1]+m-1)%(n+1-i);
        if(ans[i]<k){
            i=0;
            m++; 
        }
    } 
    Joseph[k]=m;
    cout<<Joseph[k]<<endl; 
}
}

测试数据有重复的,比如输入13,再输入13,所以要用一个表存起来,防止爆时间,这就是传说中的骚操作……

附上我的链表代码:
解决10以内的还是不成问题

#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
typedef Node* myNode;
void deleted(myNode &newhead) 
{ 
myNode temp;
while(newhead->next!=newhead)
{
    temp=newhead->next;
    newhead->next=newhead->next->next;
    delete temp;
}
delete newhead->next;
delete newhead;
}
myNode build(int k)
{
myNode head,p,q;
head=new Node;
q=new Node;
head->data=1;
head->next=NULL;
p=head;
for(int i=1;i<2*k;i++)
    {
        q=new Node;
        q->data=i+1;
        q->next=p->next;
        p->next=q;
        p=p->next;
    }
p->next=head;
return head;
}
int kill(myNode &newhead,int m,int k)
{
int j=1;
while(j<m-1){
    newhead=newhead->next;
    j++;
}
myNode q;
q=newhead->next;
int temp=q->data;
newhead->next=newhead->next->next;
newhead=newhead->next;
delete q;
return temp<=k;//返回kill 的人编号是1还是0   
}
int main()
{
int k;
int hasfound=0;//如果找到合适的m 则变为1 
while(cin>>k){
    if(k==0)
        break;
    int flag,m;
    myNode newhead;
    for(m=k+1;hasfound==0;m++)
    {
        cout<<m<<endl;
        newhead=build(k);
        for(int i=0;i<k;i++)
            {
                flag=0;
                flag=kill(newhead,m,k);
                if(flag==1)
                    break;
            }
        if(flag==0)
            hasfound=1; 
    }
    cout<<m-1<<endl;
    hasfound=0; 
    deleted(newhead);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值