题目链接:C-木棍游戏_牛客小白月赛43 (nowcoder.com)

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include <iostream>
#include<stack>
#include<cstdlib>
#include<map>
#pragma warning(disable:4996) 
using namespace std;
typedef long long ll;
//总结 本题DFS暴力每一种情况
//A表示A边的和
//B表示B边的和
//C表示C边的和
//最后通过面积公式,求面积最大即可
double gets(int a, int b, int c)
{
    double s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}
double ans = -1;
int n = 0;
double A = 0;
double B = 0;
double C = 0;
int a[100];
void dfs(int now)
{
    if (now == n + 1)
    {
        if (A + B > C && B + C > A && A + C > B)
        {
            double s = gets(A, B, C);
            ans = max(ans, s);
        }
        return;
       
    }
    dfs(now + 1);
    A += a[now];
    dfs(now + 1);
    A -= a[now];
    B += a[now];
    dfs(now + 1);
    B -= a[now];
    C += a[now];
    dfs(now + 1);
    C -= a[now];
}
int main()
{
    
    cin >> n;
    int i = 0;
    for (i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    dfs(1);
    if (ans <= 0)
    {
        cout << -1;
    }
    else
    {
        printf("%.1lf", ans);
    }
    
    return 0;
}

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐