POJ 1012-Joseph
来源:互联网 发布:南京大学软件研究生院 编辑:程序博客网 时间:2024/05/16 13:38
Joseph
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 47657 Accepted: 17949
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.
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
340
Sample Output
530
小结:
这题目我还是很无语的,一开始就没意识到这是水题,打算用搜索来,想了半天总是不对,后面听别人说了说解法,真想跳楼...
以下是AC代码:
暴力解法:
#include <stdio.h>#include <string.h>int main(){ int k; int ans[20]; int Joseph[20]={0}; for(k=1;k<14;k++) { int m=1; memset(ans,0,sizeof(ans)); for(int i=1;i<=k;i++) { ans[i]=(ans[i-1]+m-1)%(2*k-i+1); if(ans[i]<k) { i=0; m++; } } Joseph[k]=m; } while(scanf("%d",&k)&&k!=0) { printf("%d\n",Joseph[k]); } return 0;}
0ms解法:(呵呵,我感觉能用这种方法的人将来一定很有前途!!!!)#include <stdio.h>#include <string.h>int main(){ int k; int Joseph[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,1245064}; while(scanf("%d",&k)&&k!=0) { printf("%d\n",Joseph[k]); } return 0;}
0 0
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- poj 1012 Joseph
- POJ 1012 Joseph
- poj 1012 Joseph
- 【POJ】1012 Joseph
- poj 1012 Joseph
- poj-1012 Joseph
- POJ 1012 Joseph
- poj 1012 Joseph
- Poj 1012 Joseph
- POJ - 1012 Joseph
- [POJ][1012]Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph(打表题)
- 线程同步:生产者与消费者模型
- 主成分分析(PCA)原理详解
- 关于23种设计模式的有趣见解
- WIN8.1下运行VC6.0就报错
- css知多少(8)——float上篇
- POJ 1012-Joseph
- OpenCV标定相机的测试程序.
- git stash的使用
- >>>在java中的用法
- Linux下Mysql数据库修改root密码、新增用户和赋权等操作
- 标准化降水指数(SPI)计算程序
- maven有关resource资源的管理和访问
- 循环-10. 求序列前N项和(15)
- TOJ 3162 Hay For Sale --DFS