java实现取字母组成串

** 取字母组成串**

A B C D中取5次,每个字母都可以重复取出,形成一个串。
现在要求,串中A出现的次数必须为偶数(0次也算偶数)。
求可以形成多少种可能的串。

参考答案:
528

public class Main1 {
    
    public static boolean judge(int[] A) {
        int count = 0;
        for(int i = 0;i < A.length;i++) {
            if(A[i] == 1)
                count++;
        }
        if(count == 0 || count % 2 == 0)
            return true;
        return false;
    }
    
    public static void main(String[] args) {
        int[] A = {1,2,3,4};  //1234分别代表ABCD
        int[] tempA = new int[5];
        int count = 0;
        for(int a = 0;a < 4;a++) {
            tempA[0] = A[a];
            for(int b = 0;b < 4;b++) {
                tempA[1] = A[b];
                for(int c = 0;c < 4;c++) {
                    tempA[2] = A[c];
                    for(int d = 0;d < 4;d++) {
                        tempA[3] = A[d];
                        for(int e = 0;e < 4;e++) {
                            tempA[4] = A[e];
                            if(judge(tempA))
                                count++;
                        }
                    }
                }
            }
        }
        System.out.println(count);
    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/12947314.html