-
Notifications
You must be signed in to change notification settings - Fork 1
/
Weird-Stock.cpp
52 lines (37 loc) · 867 Bytes
/
Weird-Stock.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
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int n,m;
//n - initial price of stock
//m - Desired price of stock
cin>>n>>m;
//Stock reduces by 1$ or doubles itself
double days = 0;
if(n == m){
days = 0;
}
else if(n > m){ // we have to reduce stock price
days = n-m;
}
else{ // we have to increase stock price
double p = ceil((log10(m) - log10(n))/(log10(2)));
if(pow(2, p) * n == m){
days = p;
}
else{
days = p + (pow(2, p) * n) - m - 1;
}
}
cout<<days;
//sol2
/*
int res = 0;
while(n < m){
m = m & 1 ? m + 1 : m / 2;
res++;
}
cout << res + (n - m) << endl;
*/
return 0;
}