您的位置:首页 > Web前端 > AngularJS

Triangular Pastures

2013-09-09 20:44 232 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: