Skip to content

Instantly share code, notes, and snippets.

@Ashaba
Last active April 16, 2019 16:13
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 Ashaba/46aad92cd80dc570b5f1c4576398ab56 to your computer and use it in GitHub Desktop.
Save Ashaba/46aad92cd80dc570b5f1c4576398ab56 to your computer and use it in GitHub Desktop.
package challenge;
/**
* You have 10,000/= bob, and you need to buy exactly 100 animals:
* - cows @1000/-
* - pigs @200/-
* - chicken @50/-
*
* 1. You must have at least one of each animal
* 2. You must use up all Ksh. 10K
* 3. You must buy exactly 100 animals
*
* How many animals can be bought
*/
public class Animals {
public static void ComputePurchase(int Totalamount) {
int[] AnimalPrices = new int[]{ 1000, 200, 50 };
String[] Animals = new String[]{"Cows", "Pigs", "Chicken"};
int[] noteCounter = new int[3];
for (int i = 0; i < 3; i++) {
if (Totalamount >= AnimalPrices[i]) {
noteCounter[i] = Totalamount / AnimalPrices[i];
Totalamount = Totalamount - noteCounter[i] * AnimalPrices[i];
}
}
// Print the combination of Animals to be bought
System.out.println("Animals to Purchase ->");
for (int i = 0; i < 3; i++) {
System.out.println(Animals[i] + " : "
+ noteCounter[i]);
}
}
public static void main(String[] args){
int amount = 10000;
ComputePurchase(amount);
}
}
@Ashaba
Copy link
Author

Ashaba commented Apr 16, 2019

OutPut:

Cows : 10
Pigs : 0
Chicken : 0```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment