[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.
Problem illustration
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); 
   合在一起的意思就是,输出一个右对齐的小数点后两位的浮点数。

参考:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值