暴力枚举

24点

http://www.nowcoder.com/questionTerminal/263fa05acac5424a91214694a1c1eb8f

import java.util.Scanner;

public class Main {

public static int count;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int[] nums = new int[24];
    for (int i = 1; i <= n; i++) {
        int index = scanner.nextInt();
        if (index < 1 || index > 23) {
            continue;
        }
        nums[index]++;
    }
    for (int i = 1; i < 13; i++) {
        for (int j = 1; j <= nums[i]; j++) {
            if (i * j > 24) {
                break;
            } else if (i * j == 24) {
                count++;
                break;
            } else {
                count(nums, i + 1, 24 - i * j);
            }
        }
    }
    System.out.println(count);
    scanner.close();
}

public static void count(int[] nums, int startIndex, int sum) {
    if (startIndex == nums.length) {
        return;
    }
    for (int j = 0; j <= nums[startIndex]; j++) {
        if (startIndex * j > sum) {
            break;
        } else if (startIndex * j == sum) {
            count++;
            break;
        } else {
            count(nums, startIndex + 1, sum - startIndex * j);
        }
    }
}

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务