Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class Kodune_5a_yl1 {
- /**
- * Töötab ainult tagurpidi :D
- * @param rahaühikud
- * @param summa
- * @param tee
- * @param teeSumma
- * @return
- */
- static boolean müntidegaTasumine(ArrayList<Integer> rahaühikud, int summa, List<Integer> tee, int teeSumma) {
- if (summa == teeSumma) {
- System.out.println(tee);
- return true;
- } else if (teeSumma < summa) {
- int viimaneIndeks = rahaühikud.size() - 1;
- int viimaneElement = rahaühikud.get(viimaneIndeks);
- rahaühikud.remove(viimaneIndeks);
- tee.add(viimaneElement);
- return müntidegaTasumine(rahaühikud, summa, tee, teeSumma + viimaneElement);
- } else {
- return false;
- }
- }
- public static void main(String[] args) {
- ArrayList<Integer> rahaühikud = new ArrayList<>(){{
- add(1);
- add(5);
- add(10);
- add(30);
- add(50);
- add(200);
- }};
- if (müntidegaTasumine(rahaühikud, 120, new ArrayList<>(), 0)) {
- System.out.println("nice");
- } else {
- System.out.println("badikas");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement