Zu Chongzhi (429–500) was a prominent Chinese mathematician and astronomer during the Liu Song and Southern Qi Dynasties. Zu calculated the value ofπ to the precision of six decimal places and for a thousand years thereafter no subsequent mathematician computed a value this precise. Zu calculated one year as 365.24281481 days, which is very close to 365.24219878 days as we know today. He also worked on deducing the formula for the volume of a sphere.
It is said in some legend story books that when Zu was a little boy, he liked mathematical games. One day, his father gave him some wood sticks as toys. Zu Chongzhi found a interesting problem using them. He wanted to make some triangles by those sticks, and he wanted the total area of all triangles he made to be as large as possible. The rules were :
1) A triangle could only consist of 3 sticks.
2) A triangle's vertexes must be end points of sticks. A triangle's vertex couldn't be in the middle of a stick.
3) Zu didn't have to use all sticks.
Unfortunately, Zu didn't solve that problem because it was an algorithm problem rather than a mathematical problem. You can't solve that problem without a computer if there are too many sticks. So please bring your computer and go back to Zu's time to help him so that maybe you can change the history.
Input
There are no more than 10 test cases. For each case:
The first line is an integer N(3 <= N<= 12), indicating the number of sticks Zu Chongzhi had got. The second line contains N integers, meaning the length of N sticks. The length of a stick is no more than 100. The input ends with N = 0.
Output
For each test case, output the maximum total area of triangles Zu could make. Round the result to 2 digits after decimal point. If Zu couldn't make any triangle, print 0.00 .
Sample Input
3
1 1 20
7
3 4 5 3 4 5 90
0
Sample Output
0.00
13.64
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const double eps=1e-9;
double v[100];
double has[100];
double get_area(double a,double b,double c){
//求角度,再求面积
/*
double th=acos((a*a+b*b-c*c)/(a*b*2));
return 0.5*a*b*sin(th);
*/
//海伦公式,直接求面积.----> 边长越大,面积越大
double p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n){
rep(i,0,n)scanf("%lf",&v[i]);
sort(v,v+n);
double ans=0;
for(int i=n-1;i>=0;){
if(i>=2){
if(v[i-2]+v[i-1]>v[i]){
ans+=get_area(v[i-2],v[i-1],v[i]);
i-=3;
}
else i--;
}
else break;
}
printf("%.2f\n",ans);
}
return 0;
}
状压dp,也是一个思路,毕竟,能暴力出来的,肯定可以用状压实现,而且常数极小
不过思路可能不是很好想
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const double eps=1e-9;
const int N=10010;
double v[20];
double dp[N];
double get_area(double a,double b,double c){
if(a+b-c<eps)return 0;
double p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
struct Node{
int sta;double res;
Node(int _sta=0,double _res=0){
sta=_sta,res=_res;
}
}vec[3000];
int main()
{
int n;
while(scanf("%d",&n)==1&&n){
rep(i,0,N)dp[i]=0;
rep(i,0,n)scanf("%lf",&v[i]);
sort(v,v+n);
int cnt=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
int t=(1<<i)|(1<<j)|(1<<k);
//printf("t:%d\n",t);
dp[t]=get_area(v[i],v[j],v[k]);
//printf("**%.2f\n",dp[t]);
vec[cnt++]=Node(t,dp[t]);
}
}
}
//printf("cnt:%d\n",cnt);
//rep(i,0,cnt)printf("i:%d %.2f\n",i,vec[i].res);
int sz=1<<n;
for(int i=0;i<sz;i++){
for(int j=0;j<cnt;j++){
if(i&vec[j].sta)continue;
dp[i|vec[j].sta]=max(dp[i|vec[j].sta],dp[i]+vec[j].res);
}
}
//for(int i=0;i<sz;i++)printf("* %.2f\n",dp[i]);
printf("%.2f\n",dp[sz-1]);
}
return 0;
}