Save the Prisoner!
Save the Prisoner!
A jail has prisoners, and each prisoner has a unique id number, , ranging from to . There are sweets that must be distributed to the prisoners.
The jailer decides the fairest way to do this is by sitting the prisoners down in a circle (ordered by ascending ), and then, starting with some random , distribute one candy at a time to each sequentially numbered prisoner until all candies are distributed. For example, if the jailer picks prisoner , then his distribution order would be until all sweets are distributed.
But wait—there's a catch—the very last sweet is poisoned! Can you find and print the ID number of the last prisoner to receive a sweet so he can be warned?
--Solution---
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t,x,y,z,i;
long int j;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d%d%d",&x,&y,&z);
j=y+z-1;
if(j>x)
j=j%x;
if(j==0)
printf("%d\n",x);
else
printf("%ld\n",j);
}
return 0;
}
Comments
Post a Comment