HDU 5135.Little Zu Chongzhi's Triangles-字符串 (2014ACM/ICPC亚洲区广州站-重现赛)

Little Zu Chongzhi's Triangles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 2515    Accepted Submission(s): 1427


Problem Description
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
 
Source
 
Recommend
liuyiding
 
 
 
大佬的代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<stack>
 7 #include<map>
 8 #include<vector>
 9 #include<queue>
10 using namespace std;
11 const int MAXN=1e5+10;
12 const double eps=1e-4;
13 const int mod=1e9+7;
14 #define INF 0x7fffffff
15 #define ll long long
16 #define edl putchar('
')
17 #define useit  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
18 #define FOR(i,a,b) for(int i=a;i<=b;i++)
19 #define ROF(i,a,b) for(int i=a;i>=b;i--)
20 #define mst(a) memset(a,0,sizeof(a))
21 #define mstn(a,n) memset(a,n,sizeof(a))
22 //struct num{int a,i;}a[MAX];
23 //bool cmp(const num &a,const num &b){return a.a>b.a;}
24 //double cross(point a,point b){return (a.x*b.y-a.y*b.x);}
25 //double dot(point a,point b){return (a.x*b.x+a.y*b.y);}
26 float ans,a[20];
27 int b[20],n;
28 float solve(float a,float b,float c)
29 {
30     float p=(a+b+c)/2;
31     if(b>c)
32     swap(b,c);
33     if(a>b)
34     swap(a,b);
35     if(b>c)
36     swap(b,c);
37     if((b+a)<=c)
38     return 0.00;
39     else
40     return sqrt(p*(p-a)*(p-b)*(p-c));
41 }
42 
43 void dfs(int time,float are)
44 {
45     if(time==0)
46     ans=max(ans,are);
47     else
48     {
49         FOR(i,1,n)
50         {
51             if(b[i])continue;
52             else
53             FOR(j,i+1,n)
54             {
55                 if(j==i||b[j])continue;
56                 else
57                 FOR(k,j+1,n)
58                 {
59                     if(k==i||k==j||b[k])continue;
60                     else
61                     {
62                         b[i]=1,b[j]=1,b[k]=1;
63                         dfs(time-1,are+solve(a[i],a[j],a[k]));
64                         b[i]=0,b[j]=0,b[k]=0;
65                     }
66                 }
67             }
68         }
69     }
70 }
71 
72 int main()
73 {
74     while(scanf("%d",&n)&&n)
75     {
76         ans=0.00;
77         FOR(i,1,n)
78         scanf("%f",&a[i]),b[i]=0;
79         dfs(n/3,0);
80         printf("%.2f
",ans);
81     }
82 }
原文地址:https://www.cnblogs.com/ZERO-/p/9695694.html