Plotters have barbarously hammered N nails into an innocent plane shape, so that one can see now only heads. Moreover, pursuing their mean object, they have hammered all the nails into the vertices of a convex polygon. After that they…it is awful… have roped off the nails, so that the shape felt upset (the rope was very thin). They’ve done it as it is shown in the figure.

Your task is to find out a length of the rope.
Input
There two numbers in the first line of the standard input: N — a number of nails (1 ≤ N ≤ 16), and a real number R — a radius of heads of nails. All the heads have the same radius. Further there are N lines, each of them contains a pair of real coordinates (separated by a space) of centers of nails. An absolute value of the coordinates doesn’t exceed 100. The nails are described either in a clockwise or in a counterclockwise order starting from an arbitrary nail. Heads of different nails don’t overlap.
Output
Output a real number with two digits precision (after a decimal point) — a length of the rope.
Example
| input | output | |
|---|---|---|
| 0.0 | 0.0 | 14.28 |
| 2.0 | 0.0 | |
| 2.0 | 2.0 | |
| 0.0 | 2.0 |
题意:
给出钉子的个数N以及钉子的半径R
给出N个钉子的位置坐标(给出的坐标是顺序的,按顺序求两点间距离)
计算围绕钉子一圈的绳子的长度
思路:
绳子长度分直线部分和转弯部分(绕钉子的一小小部分)
计算钉子与钉子之间的距离,再进行求和,即绳子的直线部分长度
所有钉子都有一段转弯部分,所有钉子的转弯部分加起来就是一个半径为钉子半径的圆⚪的周长
Ps:注意输入的半径长可能为double型数字,int定义是错误的
代码:
#include<stdio.h>
#include<math.h>
double a[20],b[20],c[20];
double pi=3.1415926;
int main()
{
int n;
double m;
scanf("%d %lf",&n,&m);
for(int i=1; i<=n; i++)
scanf("%lf %lf",&a[i],&b[i]);
int j=0;
for(int i=1; i<n; i++)
{
c[j++]=sqrt((a[i+1]-a[i])*(a[i+1]-a[i])+(b[i+1]-b[i])*(b[i+1]-b[i]));
}
c[j++]=sqrt((a[1]-a[n])*(a[1]-a[n])+(b[1]-b[n])*(b[1]-b[n]));//计算最后一个钉子与第一个钉子之间的距离
double s=0;
for(int i=0;i<j;i++)
{
s+=c[i];
}
s+=2*pi*m;
printf("%.2f\n",s);
return 0;
}
博客围绕计算绕钉子一圈绳子的长度展开。输入钉子个数、半径及各钉子坐标,需按顺序求两点间距离。思路是将绳子长度分为直线和转弯部分,直线部分为钉子间距离之和,转弯部分总和是半径为钉子半径的圆周长。最后给出代码实现。
493





