4557. MUSICAL CHAIRS
PROBLEM CODE: ANARC08F
In the traditional game of Musical Chairs, N + 1 children run around N
chairs (placed in a circle) as long as music is playing. The moment the
music stops, children run and try to sit on an available chair. The
child still standing leaves the game, a chair is removed, and the game
continues with N children. The last child to sit is the winner.
In an attempt to create a similar game on these days' game consoles, you modify the game in the following manner: N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N . Your program pre-selects a positive number D . The program starts going in circles counting the children starting with the first chair. Once the count reaches D , that child leaves the game, removing his/her chair. The program starts counting again, beginning with the next chair in the circle. The last child remaining in the circle is the winner.
In an attempt to create a similar game on these days' game consoles, you modify the game in the following manner: N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N . Your program pre-selects a positive number D . The program starts going in circles counting the children starting with the first chair. Once the count reaches D , that child leaves the game, removing his/her chair. The program starts counting again, beginning with the next chair in the circle. The last child remaining in the circle is the winner.
Input
Your program will be tested on one or more test cases. Each test case specifies two positive integers N and D on a single line, separated by one or more spaces, where N, D < 1,000,000 .The last line of the input file contains two 0's and is not part of the test cases.
Output
For each test case, write the winner using the following format:N D W
Where N and D are as above, is a space character, and W is the winner of that game.
Example
Input: 5 3 7 4 0 0 Output: 5 3 4 7 4 2
SOLUTION-----------------------------------------------------------------------//
#includeint main() { long long int N,K,W,i; scanf("%lld",&N); scanf("%lld",&K); while(N!=0||K!=0) { W=0; for(i=2;i<=N;i++) { W=(W+K)%i; } printf("%lld %lld %lld\n",N,K,W+1); scanf("%lld",&N); scanf("%lld",&K); } return 0; }
No comments:
Post a Comment