Recently I saw an interview question from an innovative factory, which is very interesting. The following shows the Algorithm Implementation (Java code ). If you have a better algorithm, follow the instructions.
ABCDE five-member fishing and sleep. A wakes up, throws one fish, divides the remaining fish into five points, and takes one copy. B wakes up and throws one, divide the remaining parts into five portions and take one portion. Then CDE picks up the fish according to the above method. Ask them how many fish they have played, write programs and algorithms.
There are many results for the total number of fish. The result of the minimum number of fish hits is 3121 fish (these five people should ask, what tools have been used to beat so many fish)
You can use a calculator to verify that 3121 is correct.
A: (3121-1)/5 = 625
Similarly, the number of fish obtained by bcde (excluding the fish they threw away) B: 499 C: 399 D: 319 E: 255
The simplest method for this question is enumeration. Starting from the minimum value, let's look at the code (implemented in Java ).
Public classTest
{
Public static voidMain (string [] ARGs)
{
// Save the number of fish obtained from A to E separately (for convenience, including the fish thrown away)
Int[] Everybody_fish =New int[5];
// Temporary array, save the remainder of the number of current fish minus 1, except 5, and only 0 meets the condition
Int[] Temp =New int[5];
// Scan from 1 to 10000
For(IntX = 1; x <= 10000; X ++)
{
// How many fish have been taken away (including thrown fish)
IntSum = 0;
IntI = 0;
// Calculate the number of fish obtained by ABCDE
For(I = 0; I <everybody_fish.length; I ++)
{
Temp [I] = (X-1-sum) % everybody_fish.length;
// The condition is not met as long as one person cannot evenly allocate the remaining fish
If(Temp [I]! = 0)
Break;
Everybody_fish [I] = (X-1-sum)/everybody_fish.length + 1;
Sum + = everybody_fish [I];
}
// The for loop ends with the normal party. If the condition is met, the corresponding value is output.
If(I = everybody_fish.length)
{
System.Out. Print ("caught" + x + "fish ");
For(I = 0; I <everybody_fish.length; I ++)
{
System.Out. Print ((Char) ('A' + I) + ":"
+ (Everybody_fish [I]-1) + "");
}
System.Out. Print ("last remaining" + (X-sum) + "fish ");
System.Out. Println ("throwing" + everybody_fish.length + "fish ");
}
}
}
}
Run the above Code and output the following three lines of information:
A total of 3121 fish caught a: 624 B: 499 C: 399 D: 319 E: 255 finally the remaining 1020 fish threw 5 fish
A total of 6246 fish caught a: 1249 B: 999 C: 799 D: 639 E: 511 finally the remaining 2044 fish threw 5 fish
A total of 9371 fish caught a: 1874 B: 1499 C: 1199 D: 959 E: 767 finally the remaining 3068 fish threw 5 fish
Within 10000, only three numbers meet this condition. The above code is generic. For example, if you change the length of two arrays to 6 and 10000 to 50000, the following information is output.
A total of 46651 fish caught a: 7775 B: 6479 C: 5399 D: 4499 E: 3749 F: 3124 Finally, the remaining 15620 fish threw 6 fish
That is to say, there are 6 people in total. Each person also throws a fish first, and then takes the remaining fish for 6 equal points. Within 50000, only 46651 meets this condition.
In this question, only the following code is the core algorithm, and others are the code for enumeration and output results.
For (I = 0; I <everybody_fish.length; I ++)
{
Temp [I] = (X-1-sum) % everybody_fish.length;
// The condition is not met as long as one person cannot evenly allocate the remaining fish
If (temp [I]! = 0)
Break;
Everybody_fish [I] = (X-1-sum)/everybody_fish.length + 1;
Sum + = everybody_fish [I];
}