简体   繁体   中英

Java program to find the number of combinations that four cards in a deck will equal 24

I have a program in Java that will take a deck of cards and calculate the number of combinations of four cards will equal 24. So far, I have this:

public class Assign5b {
    public static void main(String[] args) {
        int[] deck = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

        int total;
        total = calculate(deck);
        output(total);
    }

    public static int calculate(int[] deck) {
        int total = 0;
        int stack1, stack2, stack3, stack4, accumulate;

        for (stack1 = 0; stack1 < 52; stack1++) {
            for (stack2 = (stack1 + 1); stack2 < 52; stack2++) {
                for (stack3 = (stack2 + 1); stack3 < 52; stack3++) {
                    for (stack4 = (stack3 + 1); stack4 < 52; stack4++) {
                        accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
                        if (accumulate == 24)
                            total++;
                    }
                }
            }
        }       
        return total;
    }   

    public static void output(int total){
        System.out.println ("The total number of card combinations of 4 that \n" + "equal 24 is: " + total);
    }
}

So my problem is not that it doesn't run, or that it's not displaying the correct value, but it's those nested loops. I don't want them there. It's making it run really inefficiently and I know there's a better way to get it to run I just can't visualize it.

You could add checks at the beginning of each loop to see if the running total already be over 24. In this case, there would be no point in doing those loops anyway:

public static int calculate(int[] deck, int target) {
    int total = 0;
    int stack1, stack2, stack3, stack4, accumulate;

    for (stack1 = 0; stack1 < 52; stack1++) {
        if (deck[stack1] > target) break;

        for (stack2 = (stack1 + 1); stack2 < 52; stack2++) {
            if (deck[stack1] + deck[stack2] > target) break;

            for (stack3 = (stack2 + 1); stack3 < 52; stack3++) {
                if (deck[stack1] + deck[stack2] + deck[stack3] > target) break;

                for (stack4 = (stack3 + 1); stack4 < 52; stack4++) {
                    if (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4] > target) break;
                    accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
                    if (accumulate == 24)
                        total++;
                }
            }
        }
    }

    return total;
}

This assumes that your deck is sorted in ascending order, eg

int[] deck = {1, 1, 1, 1,
              2, 2, 2, 2,
              ...
              13, 13, 13, 13};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM