牛客 校赛T9. 对于每一个木棍, 分出 加到a, 加到b, 加到c, 不加, 四个分支

对于每一个木棍, 分出 加到a, 加到b, 加到c, 不加, 四个分支

package newCode;

import java.util.Scanner;

public class NC231992 {
    static int n;
    static int[] arr;
    static double ans = -1;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        arr = new int[n];
        for (int i = 0; i < n; i ++ ) arr[i] = sc.nextInt();
        dfs(0, 0,0, 0);
        if (ans > 0) System.out.printf("%.1f\n", ans);
        else System.out.println(-1);
    }

    public static void dfs(int a, int b, int c, int now) {
        if (now == n) {
            if (check(a, b, c)) {
                double p = (double) (a + b + c) / 2;
                ans = Math.max(ans, Math.sqrt(p * (p - a) * (p - b) * (p - c)));
            }
            return ;
        }

        dfs(a + arr[now], b, c, now + 1);
        dfs(a, b + arr[now], c, now + 1);
        dfs(a, b, c + arr[now], now + 1);
        dfs(a, b, c, now + 1);
    }

    public static boolean check(int a, int b, int c) {
        if (a + b <= c || a + c <= b || b + c <= a) return false;
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值