【模拟】【HDU1443】 Joseph

Joseph

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


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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1444 1397 1452 1725 1393 
 

模拟打表 但是写的异常慢 虽然过了 但是太慢了



代码如下
#include<stdio.h>
#include<string.h>
int m[30],a[30];
int main()
{
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	int  k,ok=1,t,i,num,flag,j;
	while(scanf("%d",&k)!=EOF&&k!=0)
	{
		if(a[k]) {printf("%d
",a[k]);continue;}
		for(i=k+1;;i++)
		{
			num=k;ok=1;flag=1;j=1;
				memset(m,0,sizeof(m));
			while(num!=0)
			{
				t=i%(num+k);
				if(t==0) t=num+k;
				for(;t!=0;j++)
				{
					if(j>2*k) j=j%(2*k);					
					if(m[j]==0) t--;
				}
				j--;
				if(1<=j&&j<=k){ok=0;break;}
				else { m[j]=1;num--;}
			}
			
			if(ok) {a[k]=i; printf("%d
",i);break;}
		}
	}
}
翻了翻 DISCUSS 貌似有个写的十分优美的
#include <cstdlib>
#include <iostream>
#include<string>
#include <cstdio>
#include<algorithm>
#include <sstream>
#include <math.h>
using namespace std;
int n, a, b;
int con[15];
int run(int a, int b)
{
	int cnt = 0;
	int sum = 2 * a;
	int index = b % (2 * a);
	if(index == 0) index = sum;
	
	while(index > a)
	{
		sum--;
		index = (index + b - 1) % sum;
		if(index == 0) index = sum;
		cnt ++;
	}
	return cnt;
}

int solve(int a)
{
	for(int i=a+1; ; i++)
	{
		if(run(a, i) == a) return i;
	}
	
}
void init()
{
	for(int i=1; i<=14; i++)
	{
		con[i] = solve(i);
	}
}

int main()
{
//freopen("D:\in.txt","r",stdin);
	init();
	while(cin>>n)
	{
		if(n == 0) break;
		else cout<<con[n]<<endl;
	}
	return 0;
}
这段代码的区别就是 将所有坏人都看做一样的人,5 6 7 8是没有区别的 当7死后 8可以被当做7 用while(index>a)保证只有坏人出局
原文地址:https://www.cnblogs.com/zy691357966/p/5480480.html