#include<stdio.h>
#define DEBUG_IO (0)
const int MAX = 1001;
typedef struct Node
{
Node* next;
int x;
Node():next(0),x(0){}
};
int main()
{
#if DEBUG_IO
freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
#endif
int test_case = 0;
scanf("%d", &test_case);
while(test_case != 0)
{
test_case--;
int i;
int monkey[MAX] = {0};
Node node[MAX];
int N, K, M;
scanf("%d %d %d", &N,&K,&M);
//建立循环链表
for(i = 1; i < N; i++)
{
node[i].x = i;
node[i].next = &node[i+1];
}
node[N].x = N;
node[N].next = &node[1];
int count = 1;
int mark = M - 1;
Node* temp;
Node* prev;
temp = &(node[K]);
//循环链表,删除节点
while(count <= N)
{
while(mark)
{
mark--;
if( M != 1)
{
prev = temp;
}
temp = temp->next;
}
if(temp->x == temp->next->x)
{
monkey[count++] = temp->x;
break;
}
if(mark == 0)
{
mark = M - 1;
monkey[count++] = temp->x;
temp = temp->next;
if(M != 1)
{
prev->next = temp;
}
}
}
printf("%d", monkey[1]);
for(i = 2; i < count - 1; i++)
{
printf(" %d", monkey[i]);
}
printf("\n");
printf("%d", monkey[count - 1]);
printf("\n");
}
return 0;
}
/**************************************************************
Problem: 1025
User: xxxxxx
Language: C++
Result: 正确
Time:20 ms
Memory:308 kb
****************************************************************/
1025约瑟夫问题
最新推荐文章于 2018-11-20 00:51:53 发布