大体题意:
有n 个人,进行比赛,第一轮比赛 从1开始报数,报到1的出局,第二轮报2,,,, 问最后谁没出局?
思路:
一看就是约瑟夫环问题的变型了。
在简单记录一下思考的过程吧:
n 个人 编号为0,1,2,,,,,n-1.
假设这一局k-1 出局了。
那么重新编号:
k k+1 k+2,,,, n-1 0 1 k-2
0 1 2 n-2
这n -2 个人进行比赛。
那么我们给他变回去即可!
怎么变呢? 显然是(x + k) % n = f[n]
但是这里k 是变化的,当i 为2 时 只有两个人 显然报数是 n-i+1.
详细见代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int f[5007];int main(){ int T; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); f[1] = 0; for (int i = 2; i <= n; ++i){ f[i] = (f[i-1] + n-i+1) % i; } PRintf("%d/n",f[n]+1); } return 0;}King's Game
Time Limit: 2000/1000 MS (java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 703 Accepted Submission(s): 390Problem DescriptionIn order to remember history, King plans to play losephus problem in the parade gap.He callsn(1≤n≤5000) soldiers, counterclockwise in a circle, in label1,2,3...n .The first round, the first person with label1 counts off, and the man who report number1 is out.The second round, the next person of the person who is out in the last round counts off, and the man who report number2 is out.The third round, the next person of the person who is out in the last round counts off, and the person who report number3 is out.The N - 1 round, the next person of the person who is out in the last round counts off, and the person who report numbern−1 is out.And the last man is survivor. Do you know the label of the survivor? InputThe first line contains a numberT(0<T≤5000) , the number of the testcases.For each test case, there are only one line, containing one integern , representing the number of players. OutputOutput exactlyT lines. For each test case, print the label of the survivor. Sample Input223 Sample Output22Hint:For test case #1:the man who report number $1$ is the man with label $1$, so the man with label $2$ is survivor.For test case #1:the man who report number $1$ is the man with label $1$, so the man with label 1 is out. Again the the man with label 2 counts $1$, the man with label $3$ counts $2$, so the man who report number $2$ is the man with label $3$. At last the man with label $2$ is survivor. SourceBestCoder Round #75 Recommendwange2014 | We have carefully selected several similar problems for you: 6014 6013 6012 6011 6010
新闻热点
疑难解答