hdu 4033Regular Polygon(二分+余弦定理)
Regular Polygon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3274 Accepted Submission(s): 996
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
The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
已知一个点到正n边形的n个顶点的距离,求正n边形的边长。
思路:
在已知的表达式中,求不出n边形的边长。但是依据两边之和大于第三边,两边之差小鱼第三边。可以得到这个边的范围.
然后由于n边形的以任意一个点,连接到所有顶点,所有的夹角之和为360,所以只需要采取二分依次来判断,是否满足。
代码:
1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<algorithm>
5 #define pi acos(-1.0)
6 #define esp 1e-8
7 using namespace std;
8 double aa[105];
9 int main()
10 {
11 int cas,n;
12 double rr,ll;
13 scanf("%d",&cas);
14 for(int i=1;i<=cas;i++)
15 {
16 scanf("%d",&n);
17 for(int j=0;j<n;j++)
18 scanf("%lf",aa+j);
19 //确定上下边界
20 ll=20001,rr=0;
21 for(int j=0;j<n;j++)
22 {
23 rr=max(rr,aa[j]+aa[(j+1)%n]);
24 ll=min(ll,fabs(aa[j]-aa[(j+1)%n]));
25 }
26 double mid,sum,cosa;
27 printf("Case %d: ",i);
28 bool tag=0;
29 while(rr>esp+ll)
30 {
31 mid=ll+(rr-ll)/2;
32 sum=0;
33 for(int j=0;j<n;j++){
34 //oosr=a*a+b*b-mid*mid; 余弦定理求夹角,然后判断所有的夹角之和是否为360
35 cosa=(aa[j]*aa[j]+aa[(j+1)%n]*aa[(j+1)%n]-mid*mid)/(2.0*aa[j]*aa[(j+1)%n]);
36 sum+=acos(cosa);
37 }
38 if(fabs(sum-2*pi)<esp){
39 tag=1;
40 printf("%.3lfn",mid);
41 break;
42 }
43 else
44 if(sum<2*pi) ll=mid;
45 else
46 rr=mid;
47 }
48 if(tag==0)
49 printf("impossiblen");
50 }
51 return 0;
52 }
- Spring Cloud构建微服务架构:服务消费(基础)【Dalston版】
- Android解析ClassLoader(一)Java中的ClassLoader
- Android学习笔记(一)之仿正点闹钟时间齿轮滑动的效果
- Android解析WindowManager(三)Window的添加过程
- Spring Cloud构建微服务架构:服务消费(Ribbon)【Dalston版】
- Android解析WindowManager(一)WindowManager体系
- ios9 http请求失败的问题
- Android内存优化(六)LeakCanary使用详解
- Spring Cloud构建微服务架构:服务消费(Feign)【Dalston版】
- React Native组件(四)TextInput组件解析
- struts2实现ajax校验的2种方法
- 单例对象
- Android+struts2+json方式模拟手机登录功能
- iOS 获取通讯录里边的电话号码AddressBook
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 自动化部署 - Laravel Deploy实战
- 正则表达式 | 锚点
- adb shell读取设置手机ocd值以及ocd介绍
- sqli-labs练习(第三、四关)
- 使用 Python 操作 word文档
- sqli-labs练习(第五、六关)
- 分享一个Qt写的SMTP邮件客户端(库)
- mysql学习笔记
- MapReduce的常见输入格式之NlineInputFormat
- MapReduce的常见输入格式之KeyValueTextInputFormat
- MapReduce之 FileInputFormat的切片策略(默认)
- MapReduce之片和块的关系
- MapReduce的常见输入格式之CombineTextInputFormat
- MapReduce之Job提交流程
- MapReduce之自定义InputFormat