Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.5 kB
1
Indexable
Never
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.Double.parseDouble;

public class Uloha {


    public static void main(String[] args) {
        String kurzy = "(1.86, 1.27, 4.5, 1.08)";
        String castky = "(0.01, 1.00, 9, 8)";

        String[] kurzyPole = kurzy.replaceAll("[,)(]","").split(" ");
        String[] castkyPole = castky.replaceAll("[(,)]","").split(" ");

        List<Double> kurzyList = Arrays.stream(kurzyPole).map(Double::parseDouble).toList();
        List<Double> castkyList = Arrays.stream(castkyPole).map(Double::parseDouble).toList();

        for (Double d : kurzyList) {
            if (d > 1 || kurzyList.size() > 10) {
                System.out.println("Neplatny input");
            }
        }
            for (Double c : castkyList) {
                if(c < 0 || castkyList.size() > 10){
                    System.out.println("Neplatny input");
                }
            }
        castkyList.forEach(System.out::println);

        for(int i = 0; i<kurzyList.size(); i++){
            if(1 > kurzyList.get(i) || kurzyList.get(i) > 10){
                System.out.println("Neplatny input. Vsetky cisla musia byt vacsia ako nula a mensie ako 11");
                break;
            }
            }
        
        List<int[]> kombo;

        double celkovyVklad = 0;
        double celkovaVyhra = 0;
        //kombo???
        for(int aku = 1; aku < castkyList.size(); aku++) {
            kombo = genKombinacie(kurzyList.size(), aku);
            for (int[] ints : kombo) {
                double kurz = 1.00;
                System.out.print("\n");
                System.out.print("(");
                for (int i : ints) {
                    System.out.print((char) (i + 65));
                    kurz *= kurzyList.get(i);
                }
                System.out.print(", ");
                System.out.print(formatNumber(formatNumber(kurz) * castkyList.get(aku))+ ", ");
                System.out.print(formatNumber(kurz)+", ");
                System.out.print(castkyList.get(aku)+ ")");

                celkovaVyhra += formatNumber(formatNumber(kurz) * castkyList.get(aku));
                celkovyVklad += castkyList.get(aku);
            }
        }
        System.out.println("\nCelkova vyhra: " + formatNumber(celkovaVyhra));
        System.out.println("Celkovy vklad: " + formatNumber(celkovyVklad));
        System.out.println("Celkovy kurz: " + formatNumber(celkovaVyhra/celkovyVklad));
    }
    
    public static List<int[]> genKombinacie(int pocetPrvkov, int pocetMiest) {
        List<int[]> kombinacie = new ArrayList<>();
        int[] kombinacia = new int[pocetMiest];
        for (int i = 0; i < pocetMiest; i++) {
            kombinacia[i] = i;
        }
        while (kombinacia[pocetMiest - 1] < pocetPrvkov) {
            kombinacie.add(kombinacia.clone());
            int c = pocetMiest - 1;
            while (c != 0 && kombinacia[c] == pocetPrvkov - pocetMiest + c) {
                c--;
            }
            kombinacia[c]++;
            for (int i = c + 1; i < pocetMiest; i++) {
                kombinacia[i] = kombinacia[i - 1] + 1;
            }
        }
        return kombinacie;
    }

        private static double formatNumber(double n){
            return parseDouble(String.format("%.2f", n).replaceAll(",", ".").replaceAll("[^0-9.]", ""));
        }
    }