博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4033 Regular Polygon(几何 + 二分)
阅读量:4670 次
发布时间:2019-06-09

本文共 3307 字,大约阅读时间需要 11 分钟。

Regular Polygon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 2203    Accepted Submission(s): 682

Problem Description

In a 2_D plane, there is a point strictly in a regular polygon with N sides. If you are given the distances between it and N vertexes of the regular polygon, can you calculate the length of reguler polygon's side? The distance is defined as dist(A, B) = sqrt( (Ax-Bx)*(Ax-Bx) + (Ay-By)*(Ay-By) ). And the distances are given counterclockwise.

Input

First a integer T (T≤ 50), indicates the number of test cases. Every test case begins with a integer N (3 ≤ N ≤ 100), which is the number of regular polygon's sides. In the second line are N float numbers, indicate the distance between the point and N vertexes of the regular polygon. All the distances are between (0, 10000), not inclusive.

Output

For the ith case, output one line “Case k: ” at first. Then for every test case, if there is such a regular polygon exist, output the side's length rounded to three digits after the decimal point, otherwise output “impossible”.

Sample Input

2
3
3.0 4.0 5.0
3
1.0 2.0 3.0

Sample Output

Case 1: 6.766
Case 2: impossible

Source

 解题报告:这道题的题意就是在多边形的内部有一个点,点到达各个顶点的距离已知,求有这样的正多边形吗;思路,是把多边形分成N个三角形,多边形的边长一定满足三角形定理,min(两边之和)> 正多边形的边长> max(两边之差);在利用二分法枚举正多边形的边,直到枚举的便满足内角和为360即可,利用余弦定理求角的大小:cosA = (a* a + b * b - c * c) / 2 * a * b;

代码如下:

#include 
#include
#include
#include
using namespace std;const double PI = acos(-1.0);const int MAX = 105;const double eps = 1e-8;double edge[MAX];int T, N;int main(){ int i, flag, k; double min, max, ans, radian; scanf("%d", &T); for (k = 1; k <= T; ++k) { memset(edge, 0, sizeof(edge)); scanf("%d", &N); for (i = 0; i < N; ++i) { scanf("%lf", &edge[i]); } edge[N] = edge[0]; max = 999999999; min = -20; double temp; for (i = 1; i <= N; ++i) { temp = edge[i] + edge[i - 1]; //利用了三角形定理,两边之和大于第三边,两边之差小于第三边 if (max > temp)//找到两边相加的最小 { max = temp; } temp = fabs(edge[i] - edge[i - 1]); if (min < temp)//找到两边相减的最大 { min = temp; } } double mid; flag = 0; while (max - min > eps)//利用二分法,枚举 { mid = (max + min) / 2.0; radian = 0;//弧度 for (i = 1; i <= N; ++i) { //利用余弦定理求弧度 radian += acos((edge[i] * edge[i] + edge[i - 1] * edge[i - 1] - mid * mid) / (2.0 * edge[i] * edge[i - 1])); } if (fabs(radian - 2.0 * PI) < eps)//存在的时候 { flag = 1; ans = mid; break; } else if (radian - 2 * PI > eps)//枚举的边大的 { max = mid; } else//枚举的边小的时候 { min = mid; } } printf("Case %d: ", k); if (flag) { printf("%.3lf\n", ans); } else { printf("impossible\n"); } } return 0;}

转载于:https://www.cnblogs.com/lidaojian/archive/2012/04/06/2435306.html

你可能感兴趣的文章
一种新的子波域滤波算法
查看>>
cookie之三天免登录代码
查看>>
1043 幸运号码 数位DP
查看>>
js18
查看>>
2018-2019-2 20175308实验一 《Java开发环境的熟悉》实验报告
查看>>
如何设置WIN7自动登录(去除登录密码)
查看>>
关于bash中if语法结构的广泛误解(转)
查看>>
10G整数文件中寻找中位数或者第K大数
查看>>
操作手机数据库的uri
查看>>
Python小应用1 - 抓取网页中的链接地址
查看>>
HTML表格和列表笔记&练习<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>关于表格的一些练...
查看>>
Hadoop HBase概念学习系列之hbase shell中执行java方法(高手必备)(二十五)
查看>>
数据类型
查看>>
SharePoint 2010中的内容类型集线器 - 内容类型发布与订阅
查看>>
如何解决在Windows Server 2008 R2 上安装证书服务重启后出现 CertificationAuthority 91错误事件...
查看>>
c# 获取键盘的输入
查看>>
mysql忘记密码
查看>>
小股神助A股股民畅享经济发展红利
查看>>
Python灰帽子pdf
查看>>
Node.js区块链开发pdf
查看>>