标签:
题目描述:
小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长。首先N个候选人围成一个圈,依次编号为0..N-1。然后随机抽选一个数K,并0号候选人开始按从1到K的顺序依次报数,N-1号候选人报数之后,又再次从0开始。当有人报到K时,这个人被淘汰,从圈里出去。下一个人从1开始重新报数。
也就是说每报K个数字,都会淘汰一人。这样经过N-1轮报数之后,圈内就只剩下1个人了,这个人就作为新的班长。
举个例子,假如有5个候选人,K=3:
初始 0: 0 1 2 3 4程序代码:
/****************************************************/
/* File : Hiho_Week_94.cpp */
/* Author : Zhang Yufei */
/* Date : 2016-04-19 */
/* Description : HihoCoder ACM program. (submit:g++)*/
/****************************************************/
#include<stdio.h>
/*
* This function deals with one test case.
* Parameters:
* None.
* Returns:
* None.
*/
void function (void);
/*
* This function get the result of the given n and k.
* Parameters:
* @n: The number of candidate.
* @k: The 'k' parameter in this question.
* Returns:
* The result for the given n and k.
*/
int compute(int n, int k);
/*
* The main program.
*/
int main (void) {
int t;
scanf("%d", &t);
for(int i = 0; i < t; i++) {
function();
}
return 0;
}
/*
* This function deals with one test case.
* Parameters:
* None.
* Returns:
* None.
*/
void function (void) {
int n, k;
scanf("%d %d", &n, &k);
printf("%d\n", compute(n, k));
}
/*
* This function get the result of the given n and k.
* Parameters:
* @n: The number of candidate.
* @k: The 'k' parameter in this question.
* Returns:
* The result for the given n and k.
*/
int compute(int n, int k) {
if(n == 1) {
return 0;
}
if(n < k) {
return (k + compute(n - 1, k)) % n;
} else {
int r = compute(n - n / k, k);
if(r < n % k) {
return r - n % k + n;
} else {
return r - n % k + (r - n % k) / (k - 1);
}
}
} 标签:
原文地址:http://blog.csdn.net/octopusflying/article/details/51204219