[timus] 1020. Rope

1020. Rope

Time Limit: 1.0 second
Memory Limit: 16 MB
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.
[timus] 1020. Rope_第1张图片
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 ≤ 100), 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 adjoin.

Output

Output a real number with two digits precision (after a decimal point) — a length of the rope.

Sample

input output
4 1
0.0 0.0
2.0 0.0
2.0 2.0
0.0 2.0

14.28
Problem Author: Alexander Petrov & Nikita Shamgunov
Problem Source: Ural State University Internal Contest October'2000 Junior Session

Solution
此题求近似凸多边形的长度,即为相邻钉子中心距离的和+钉子边缘的圆弧长度和。
以为凸多边形的内角和为360度,故圆弧面积为单个钉子的周长。
N边凸多边形内角和为360*(N-2)/2=180*(N-2),每个钉子的绳子圆弧部分圆心角与该内角互补,故圆弧总角度:
              180N-180*(N-2)=360
因此圆弧面积为单个钉子的周长。
源代码如下:
//problem 1020

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

double distance(double x1,double y1,double x2,double y2)
{
	double x3=pow((x1-x2),2);
	double y3=pow((y1-y2),2);
	return sqrt(x3+y3);
}

int main()
{
	int N;
	double R;
	cin>>N>>R;


	double *x=new double[N];
	double *y=new double[N];
	
	for(int i=0;i<N;++i)
		cin>>x[i]>>y[i];

	double answer=0,pi=3.1415926;
	for(int i=0;i<N-1;++i)
		answer+=distance(x[i],y[i],x[i+1],y[i+1]);
	answer+=distance(x[N-1],y[N-1],x[0],y[0]);
	answer+=2*pi*R;

	cout<<answer<<endl;
	cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2)<<answer; 
	system("pause");
	return 0;
}


收获:
1. math.h中指数函数pow(x,y)的使用。
2.iomanip中对double精度的控制。
    setiosflags 是包含在命名空间iomanip 中的C++ 操作符,该操作符的作用是执行由有参数指定区域内的动作;
     iso::fixed 是操作符setiosflags 的参数之一,该参数指定的动作是以带小数点的形式表示浮点数,并且在允许的精度范围内尽可能的把数字移向小数点右侧;
    iso::right 也是setiosflags 的参数,该参数的指定作用是在指定区域内右对齐输出;
    setprecision 也是包含在命名空间iomanip 中的C++ 操作符,该操作符的作用是设定浮点数;
   setprecision(2) 的意思就是小数点输出的精度,即是小数点右面的数字的个数为2。

   cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2); 
   合在一起的意思就是,输出一个右对齐的小数点后两位的浮点数。

参考:
[1]:http://acm.timus.ru/problem.aspx?space=1&num=1020
[2]:http://zhidao.baidu.com/question/52111153.html
[3]: http://topic.csdn.net/u/20090528/13/c37f60e9-86bb-4618-a7bf-d2c311c7345b.html
[4]: http://zhidao.baidu.com/question/90059488.html


你可能感兴趣的:(ios,object,pair,distance,Numbers,math.h)