FENCE1 - Build a Fence editorial SPOJ - UCS - Unleash-Coding-Skills

Wednesday, 2 May 2018

FENCE1 - Build a Fence editorial SPOJ - UCS


The link for the problem FENCE1 - Build a Fence :

http://www.spoj.com/problems/FENCE1/

This problem is an ad-hoc one where we need to find the maximizing area such that endpoints touch the wall and to have maximum area. In this case, we know the semi-circle will make the maximum area.

The given input is the length of arc 'l'

We know that perimeter of a semicircle is:

l = (PIE) * R
 So know we can find R= l/(PIE)

Now the area of the semicircle is ((PIE) *R * R/ 2 ) by substituting R from above equation we get

 Area= (L*L)/(2*(PIE))

Where PIE = 3.1415926

So know we can apply this to our problem FENCE1 - Build a Fence.

The following is CPP solution for the problem FENCE1 - Build a Fence :

#include <bits/stdc++.h>
using namespace std;
int main() {
     int num;
     scanf("%d",&num);
     while(num)
     {
          float res=0;
          res=(1/2.0)*(num*num)*(1/3.1415926);
          printf("%.2f\n",res);
          scanf("%d",&num);   
     }
     return 0;
}

Happy Coding...............

No comments:

Post a comment