Skip to content

Instantly share code, notes, and snippets.

@todvora
Created October 21, 2014 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todvora/524e1d95227016c7ec1f to your computer and use it in GitHub Desktop.
Save todvora/524e1d95227016c7ec1f to your computer and use it in GitHub Desktop.
Chickens
/**
* https://plus.google.com/114134834346472219368/posts/CR1ZoNe9ojQ
*
* Three farmers were selling chickens at the local market.
* One farmer had 10 chickens to sell, another had 16 chickens to sell,
* and the last had 26 chickens to sell. In order not to compete with
* each other, they agreed to all sell their chickens at the same price.
* But by lunchtime, they decided that sales were not going so well,
* and they all decided to lower their prices to the same lower price point.
* By the end of the day, they had sold all their chickens.
* It turned out that they all collected the same amount of money,
* $35, from the day's chicken sales.
*
* What was the price of the chickens before lunchtime and after lunchtime?
*/
public class Chickens {
public static final int TOTAL_INTAKE = 3500; // in cents
public static final int TOTAL_CHICKENS_A = 10;
public static final int TOTAL_CHICKENS_B = 16;
public static final int TOTAL_CHICKENS_C = 26;
public static void main(String[] args) {
LOOP:
for (int i = 0; i <= TOTAL_CHICKENS_A; i++) {
int j = TOTAL_CHICKENS_A - i;
for (int k = 0; k <= TOTAL_CHICKENS_B; k++) {
int l = TOTAL_CHICKENS_B - k;
for (int m = 0; m <= TOTAL_CHICKENS_C; m++) {
int n = TOTAL_CHICKENS_C - m;
for (int x = 1; x <= TOTAL_INTAKE; x++) {
for (int y = 1; y < x; y++) {
if (
i * x + j * y == TOTAL_INTAKE &&
k * x + l * y == TOTAL_INTAKE &&
m * x + n * y == TOTAL_INTAKE
) {
System.out.println(String.format("First price was %.2f$, and then %.2f$", x / 100.0, y / 100.0));
System.out.println(String.format("Farmer A sold %d, and then %d chickens", i, j));
System.out.println(String.format("Farmer B sold %d, and then %d chickens", k, l));
System.out.println(String.format("Farmer C sold %d, and then %d chickens", m, n));
break LOOP;
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment