-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquareRoot.cpp
executable file
·133 lines (121 loc) · 4.53 KB
/
squareRoot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*Ghost Return*/
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <math.h>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <new>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
/******************************************/
/** Author : Rafik Farhad */
/** Mail to : [email protected] */
/** Created : 2017-03-10-22.27 */
/******************************************/
#define CLR(o) memset(o, 0x00, sizeof o)
#define CLR1(o) memset(o, -1, sizeof o)
#define takei(a) scanf("%d", &a)
#define takell(a) scanf("%lld", &a)
#define takellu(a) scanf("%llu", &a)
#define sf scanf
#define pb push_back
#define mp make_pair
#define ppp system("pause")
#define ok cout << "OKA" <<endl;
#define pf printf
#define NL printf("\n")
#define PI 2*acos(0)
#define all(o) o.begin(), o.end()
#define csi pf("Case %d: ", ++keis)
#define csii pf("Case %d:\n", ++keis)
#define _(o) pf("%d\n", o)
#define ll long long
#define ull unsigned long long
#define exx 2.7182818284590452353602875
#define xx first
#define yy second
///Helper
using namespace std;
template <class T> T MAX(T a, T b) { return a>b?a:b;}template <class T> T MIN(T a, T b) { return a<b?a:b;}
template <class T1> void __(T1 p) { cout << p << endl;}
template <class T1> void deb(T1 p) { cout << "Debugging: " << p << endl;}
template <class T1, class T2> void deb(T1 p, T2 q) { cout << "Debugging: " << p << "\t" << q << endl;}
template <class T1, class T2, class T3> void deb(T1 p, T2 q, T3 r) { cout << "Debugging: " << p << "\t " << q << "\t " << r << endl;}
template <class T1, class T2, class T3, class T4> void deb(T1 p, T2 q, T3 r, T4 s) { cout << "Debugging: " << p << "\t " << q << "\t " << r << "\t " << s << endl;}
long long int POOW(long long b, long long p) { if(p==0) return 1; return b*POOW(b, p-1);}
//int SET(int mask, int pos){return mask singlebar (1<<pos);}
//bool CHK(int mask, int pos){return (1&(mask>>pos));}
const int xx[] = {0, 0, 1, -1, -1, 1, -1, 1};const int yy[] = {1, -1, 0, 0, 1, 1, -1, -1}; const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2}; const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1}; // KX-> Knight moves xx-> diagonal -> 8 horizontal/vertical->4
#define LT (1<<31)-1
#define MX
#define MOD
#define EPS 1e-5
//#define MY INT_MIN
//ll FAST_EXP(ll base, ll power) /*base^power%MOD*/ {ll res=1ll;while(power){if(power&1)res=(res*base)%MOD;base=(base*base)%MOD;power>>=1;}return res%MOD;}
bool is_low(double low, double high)
{
double diff = high - low;
if(diff > EPS)
{
return true;
}
return false;
}
bool is_equal(double a, double b)
{
double diff = fabs(a - b);
if(diff <= EPS)
{
return true;
}
return false;
}
int main()
{
//ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
//freopen("/home/rafikfarhad/Desktop/000.txt","r",stdin);
//freopen("000.txt","r",stdin);
//freopen("output.txt", "w", stdout);
//clock_t ooo = clock();
#endif
/// MAIN
int i, t, j, k, l, keis(0);
double c, d, x, y, a, b, low, high, mid, ans;
while(scanf("%lf", &a) != EOF)
{
low = 0;
high = a;
ans = 0;
while(is_low(low, high))
{
mid = (low+high)/2.0;
ans = mid;
if(is_equal(mid*mid, a))
{
break;
}
if(is_low(mid*mid, a))
{
low = mid + EPS;
}
else {
high = mid - EPS;
}
}
printf("%f -> %f\n", a, ans);
}
/* Coding is FUN */
/// ENDD
#ifndef ONLINE_JUDGE
//pf("-------ENDS OF OUTPUT------\n\n");
//pf("Time Elapsed: %lu\n", (clock()-ooo));
#endif
return 0;
}