首页 > 代码库 > hdu 5135 Little Zu Chongzhi's Triangles(贪心)
hdu 5135 Little Zu Chongzhi's Triangles(贪心)
Little Zu Chongzhi‘s Triangles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 296 Accepted Submission(s): 173
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.
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.
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
31 1 2073 4 5 3 4 5 900
Sample Output
0.0013.64
题意 : 给你n个木棒的长度 。 要你用木棒组成三角形(一个三角形只能
有三个木棒构成,且三角形顶点为木棒顶点),要使组成的所有三角形的
面积之和最大。输出最大值。
思路 :
海伦公式 :若 a ,b c w为三条边 ,则 面积 S = sqrt ( p * (p-a) * (p-b) * (p-c) ) ,其中 p=(a+b+c)/2;
由海伦公式知 边长越大,面积越大 。 所以将边排序, 然后从大到小地找能组成
三角形的木棒即可。具体实现看代码
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;const int maxn=15;int n,a[maxn];void input(){ for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n);}bool judge(int x,int y,int z){ if(x+y>z && x+z>y && y+z>x) return true; return false;}double Area(int x,int y,int z){ double xx=x,yy=y,zz=z; double p=(xx+yy+zz)/2.0; return sqrt(p*(p-xx)*(p-yy)*(p-zz));}void solve(){ double ans=0.0; for(int i=n-1;i>=2;) { int x=a[i],y=a[i-1],z=a[i-2]; if(judge(x,y,z)) { ans+=Area(x,y,z); i-=3; } else i--; } printf("%.2lf\n",ans);}int main(){ while(scanf("%d",&n)!=EOF) { if(n==0) break; input(); solve(); } return 0;}
hdu 5135 Little Zu Chongzhi's Triangles(贪心)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。