SRM 151 1A 2013.12.3

SRM 151 1A 2013.12.3

DIV1

250

 

Problem Statement

    

The Greek mathematician Archimedes ofSyracuse (287-212 B.C.) was most likely the first in finding a way tosystematically (iteratively) approximate the value of Pi, the ratio between theperimeter and the diameter of a circle. He inscribed and circumscribed regular polygons in/around a circle andcalculated their perimeter. These are lower (inscribed polygon) and upper(circumscribed polygon) bounds for the perimeter of the circle, and thereforecan be used to calculate lower and upper bounds for Pi. He then found a formulafor directly calculating the perimeter of inscribed and circumscribed polygonswith 2*n sides only using the values previously calculated for polygons with nsides.  You now have a slightly simplertask, considering only inscribed polygons. You are given an int numSides, thenumber of sides of a regular polygon that is inscribed in a circle. Return adouble, the approximated value for Pi when the perimeter of that polygon isused as an approximation for the perimeter of the circle.   [Image showing a circle with an 8-sidedregular polygon inscribed]

Definition

    

Class:

Archimedes

Method:

approximatePi

Parameters:

int

Returns:

double

Method signature:

double approximatePi(int numSides)

(be sure your method is public)

    

 

Notes

-

Perimeter of a circle: 2 * PI * radius

-

Perimeter of a n-sided regular polygon: n *sidelength

-

Reminder how doubles are evaluated: If yourresult is within 1e-9 of the expected result, your solution will be evaluatedas correct. If your result is between (1+1e-9) * expected and (1-1e-9) *expected, it will be evaluated as correct.

Constraints

-

numSides will be between 3 and 100000,inclusive.

Examples

0)

 

    

3

Returns: 2.598076211353316

 

1)

 

    

8

Returns: 3.0614674589207183

 

2)

 

    

17280

Returns: 3.1415926362832276

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

206.40

 

#include<iostream>

#include<string>

#include<vector>

#include<cmath>

 

#define PI 3.14159265358979323846

using namespace std;

 

class Archimedes

{

public:double approximatePi(int numSides)

         {

                      double ans;

                      ans=numSides*sin((PI)/(double)numSides);

                      return ans;

         }

};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值