
Patrick B. answered 08/28/19
Math and computer tutor/teacher
Each player gets N/3 of them.
Then you find the REMAINDER (using the mod function) and add it to one of them,
so that is the unique winner
So for 5 toys, 5/3 = 1, so each player gets one toy.
5 mod 3 = 2, so given the 2 toys to one player.
Therefore, (1,3,1), (3,1,1) or (1,1,3)
For n=7, 7/3 = 2 so each players gets 2 toys.
7 mod 3 = 1, so give the 1 extra toy to one player.
(2,3,2) (3,2,2) or (2,2,3)
Now, IF the number N is evenly divisible by 3, then
just give an extra toy to another player.
EX. N=9. Each player gets 9/3=3 toys with none
left over. So give one toy another player.
Then (4,3,2), (3,4,2), (2,3,4)
In this case, the solution shall be
(k+1,k,k-1) where k = N/3
Here's Java:
import java.io.*;
class ToyDistribution
{
private Console console;
private int numToys;
ToyDistribution()
{
console = System.console();
}
public void Go()
{
try
{
System.out.print(" how many toys ??? :> ");
String inbuff = console.readLine();
numToys = Integer.parseInt(inbuff);
int remainder = numToys % 3;
int quotient = numToys / 3;
if (remainder==0)
{
System.out.println( "(" + (quotient+1) + " , "
+ (quotient) + " , "
+ (quotient-1) + ")"
);
}
else
{
System.out.println(
"(" + (quotient) + " , "
+ (quotient + remainder) + " , "
+ (quotient) + ")"
);
}
}
catch (Exception ex)
{
System.out.println(" Error on input - sorry ");
}
}
public static void main(String args[])
{
ToyDistribution x = new ToyDistribution();
x.Go();
}
}