HDU1443 模拟(难)

Joseph

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


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
题意:
给出2*n个数串成环,找出一个k使得每隔k个数就去掉一个数,要去掉后n个数的情况下的最小的k。
代码:
  1 /*
  2 用数组和链表写的两个,费了一晚上时间一直超时。数据只有13个最后只能先跑出来数据再打表提交15S水过去。然而怎么就忘了把这两步合在一起呢?
  3 真是糊涂了。
  4 */
  5 #include<iostream>
  6 #include<cstdio>
  7 using namespace std;
  8 int main()
  9 {
 10     int linklt_point[30];
 11     int ans[15];
 12     int t;
 13     for(int n=1;n<14;n++)
 14     {
 15         if(n==0) break;
 16         for(int i=n+1;;i++)
 17         {
 18             if(i%(2*n)!=0&&i%(2*n)<=n)
 19             continue;
 20             for(int j=1;j<2*n;j++)
 21             {
 22                 linklt_point[j]=j+1;
 23             }
 24             linklt_point[2*n]=1;
 25             int sta=1,pre=2*n;
 26             int sum=0;
 27             int m=2*n;
 28             while(1)
 29             {
 30                 int k=i%m;
 31                 if(k==0) k=m;
 32                 for(int j=1;j<k;j++)
 33                 {
 34                     pre=sta;
 35                     sta=linklt_point[sta];
 36                 }
 37                 if(sta<=n)
 38                 break;
 39                 linklt_point[pre]=linklt_point[sta];
 40                 sta=linklt_point[sta];
 41                 sum++;
 42                 m--;
 43                 if(sum==n)
 44                 break;
 45             }
 46             if(sum==n)
 47             {
 48                 ans[n]=i;
 49                 break;
 50             }
 51         }
 52     }
 53     while(scanf("%d",&t)&&t!=0)
 54     {
 55         printf("%d\n",ans[t]);
 56     }
 57     return 0;
 58 }
 59 
 60 
 61 #include<iostream>
 62 #include<cstdio>
 63 using namespace std;
 64 int main()
 65 {
 66     int a[30];
 67     int n;
 68     int ans[15];
 69     for(int t=1;t<14;t++)
 70     {
 71         for(int i=t+1;;i++)
 72         {
 73             for(int j=1;j<=2*t;j++)
 74             a[j]=1;
 75             int sta=0;
 76             int sum=0;
 77             int m=2*t;
 78             while(1)
 79             {
 80                 int k=i%m;
 81                 if(k==0) k=m;
 82                 int l=0;
 83                 while(l!=k)
 84                 {
 85                     sta++;
 86                     if(sta==2*t+1)
 87                     sta=1;
 88                     if(a[sta]==1)
 89                     l++;
 90                 }
 91                 if(sta<=t)
 92                 break;
 93                 m--;
 94                 a[sta]=0;
 95                 sum++;
 96                 if(sum==t)
 97                 break;
 98             }
 99             if(sum==t)
100             {
101                 ans[t]=i;
102                 break;
103             }
104         }
105     }
106     while(scanf("%d",&n)&&n!=0)
107     {
108         printf("%d\n",ans[n]);
109     }
110     return 0;
111 }

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/5951045.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值