Triangular Pastures

本文介绍了一个编程问题,任务是寻找由给定长度的围栏组成的最大三角形牧场面积。通过动态规划方法,文章提供了一段C++代码实现,用于解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Triangular Pastures

时间限制(普通/Java):3000MS/10000MS          运行内存限制:65536KByte

描述

 

Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.

I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N (3 <= N <= 40) fence segments (each of integer length Li (1 <= Li <= 40) and must arrange them into a triangular pasture with the largest grazing area. Ms. Hei must use all the rails to create three sides of non-zero length.

Help Ms. Hei convince the rest of the herd that plenty of grazing land will be available. Calculate the largest area that may be enclosed with a supplied set of fence segments.

 

输入

* Line 1: A single integer N
* Lines 2..N+1: N lines, each with a single integer representing one fence segment's length.  The lengths are not necessarily unique.

输出

A single line with the integer that is the truncated integer representation of the largest possible enclosed area multiplied by 100. Output -1 if no triangle of positive area may be constructed.

样例输入

5
1
1
3
3
4

 

样例输出

692
 
题目大意:给出n条边,找出可以拼成的一个三角形的最大面积
 
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
bool flag[810][810];

int main() {

    int n,i,j,k,ans;
    int str[110];

    while(~scanf("%d",&n)) {
        ans = 0;
        for(i=1; i<=n; i++) {
            scanf("%d",&str[i]);
            ans+=str[i];
        }
        memset(flag,false,sizeof(flag));
        flag[0][0]=true;
        int mid=ans/2;

     //判断是否可以形成三角形
for(i=1; i<=n; i++) for(j=mid; j>=0; j--) for(k=j; k>=0; k--) if(j>=str[i]&&flag[j-str[i]][k] || k>=str[i]&&flag[j][k-str[i]]) flag[j][k]=true; int Max=-1; for(i=mid; i>=1; i--) for(j=i; j>=1; j--) { if(flag[i][j]) { k=ans-i-j; if(i+j>k && i+k>j && j+k>i) { double p=(i+j+k)/2.0; int c=(int)(sqrt(p*(p-i)*(p-j)*(p-k))*100); if(c>Max) Max=c; } } } printf("%d\n",Max); } return 0; }

 

转载于:https://www.cnblogs.com/lavender913/p/3310813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值