HDU5135 Little Zu Chongzhi's Triangles(计算几何,枚举)

2023-10-06 00:19

本文主要是介绍HDU5135 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): 1901    Accepted Submission(s): 1083


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
2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6022  6021  6020  6019  6018 

Statistic |  Submit |  Discuss |  Note
思路:
有n个数,接下来有n个小木棍,要把它尽量围成多个三角形,并且使,围成的三角形的面积最大。

需要满足三个条件:

①三角形只能有三根棒组成

②三角形的顶点必须是棒的终点

③木棍不能用完

先按照贪心的思路给边排序,然后在枚举

代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N 100
#define M 1000000
#define ll long long
using namespace std;
struct node
{double l;int vis;
} a[N];
int cmp(node a,node b)
{return a.l>b.l;
}
double mj(double a,double b,double c)
{double p=(a+b+c)/2;return sqrt(p*(p-a)*(p-b)*(p-c));
}
int jc(double a,double b,double c)
{if(a+b>c&&a+c>b&&b+c>a)return 1;elsereturn 0;
}
int main()
{int n;while(scanf("%d",&n)&&n){for(int i=1; i<=n; i++){scanf("%lf",&a[i].l);a[i].vis=0;}sort(a+1,a+n+1,cmp);double num=0.0;int flag;for(int i=1; i<=n; i++){flag=0;if(a[i].vis==0&&flag==0){for(int j=i+1; j<=n; j++){if(a[j].vis==0&&flag==0){for(int k=j+1; k<=n; k++){if(a[k].vis==0&&flag==0){if(jc(a[i].l,a[j].l,a[k].l)){//debug();num+=mj(a[i].l,a[j].l,a[k].l);a[i].vis=1;a[j].vis=1;a[k].vis=1;flag=1;break;}}}}}}}printf("%.2lf\n",num);}return 0;
}


这篇关于HDU5135 Little Zu Chongzhi's Triangles(计算几何,枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/152377

相关文章

C#实现获得某个枚举的所有名称

《C#实现获得某个枚举的所有名称》这篇文章主要为大家详细介绍了C#如何实现获得某个枚举的所有名称,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... C#中获得某个枚举的所有名称using System;using System.Collections.Generic;usi

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

Java 枚举的常用技巧汇总

《Java枚举的常用技巧汇总》在Java中,枚举类型是一种特殊的数据类型,允许定义一组固定的常量,默认情况下,toString方法返回枚举常量的名称,本文提供了一个完整的代码示例,展示了如何在Jav... 目录一、枚举的基本概念1. 什么是枚举?2. 基本枚举示例3. 枚举的优势二、枚举的高级用法1. 枚举

Rust中的Option枚举快速入门教程

《Rust中的Option枚举快速入门教程》Rust中的Option枚举用于表示可能不存在的值,提供了多种方法来处理这些值,避免了空指针异常,文章介绍了Option的定义、常见方法、使用场景以及注意事... 目录引言Option介绍Option的常见方法Option使用场景场景一:函数返回可能不存在的值场景

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <