zoukankan      html  css  js  c++  java
  • hdu 1443 Joseph (约瑟夫环)

    Joseph

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


    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

     比赛时候差一点就推出来了  自己比赛时太容易紧张了  尤其是跟队友一起时,唉  还是心态不行

    题意:总共有2*k个人站成一个环,前k个人是好人,后k个人是坏人,现在要处决这2*k个人要求后k个坏人要在好人之前死,让找一个最小的m(按照圆圈数m个人第m个人处死,然后从这个处死的人的下一位为起点继续数m个以此类推,直至杀死所有人)使得坏人先死

    题解:当前要处死的人为kill=(kill+m-1)%n   (n为当前活着的人的数量)如果kill等于0 证明是要处死第n个人所以kill=n 

    我直接用函数求解超时了  ,因为数据不大 所以我直接吧14个数打表输出了

    #include<stdio.h>
    #include<string.h>
    #include<cstdio> 
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 1100
    #define mod 100
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    int f[20]; 
    int s[20]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,13482720};
    int fun(int k,int m)
    {
    	int kill,i,j,n;
    	n=2*k;
    	kill=1;
    	for(i=1;i<=k;i++)
    	{
    		kill=(kill+m-1)%n;
    		if(kill==0)
    		    kill=n;
    		else if(kill<=k)
    		    return 0;
    		n--;
    	}
    	return 1;
    }
    int main()
    {
    	int n,m,j,i,t,k;
    	while(scanf("%d",&k),k)
    	{
    //		for(i=1;i<=14;i++)
    //		{
    //			for(j=1;;j++)
    //			{
    //				if(fun(i,j))
    //				{
    //					f[i]=j;
    //					break;
    //				}
    //			}
    //		}
    		printf("%d
    ",s[k]);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    一种循环方式
    SqlServer循环读取配置
    app抓包
    c# 前端写代码的情况
    第36月第27日 codesign重签名
    第36月第26天 吴恩达 目标检测
    第36月第25天 TensorFlow Object_detection
    第36月第19天 多个tomcat查端口
    第36月第8天 flask_bootstrap
    第36月第5天 升级到 XCode10.3 项目中的xib报错
  • 原文地址:https://www.cnblogs.com/tonghao/p/5329895.html
Copyright © 2011-2022 走看看