forked from love1024/spoj-solution-with-explanation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DANGER.cpp
51 lines (40 loc) · 1.18 KB
/
DANGER.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;
//Convert the string into required number
int convert(string &str) {
int first = (str[0]-'0')*10 + (str[1]-'0');
int second = (str[3]-'0');
//Multiple first number of given number of zeros
while(second > 0) {
first *= 10;
second--;
}
return first;
}
int main() {
//Take the input strings
string str;
cin>>str;
//While there is input
while(str != "00e0") {
//Convert the string in number
int num = convert(str);
//The Main point here is the pattern which
//repeat after every power of 2. So after every power
//of 2 we reach again at first position and after that
//the next numbers will give answer in odd
//For example for 10 . For 8 which is power of 2 answer is 1
//for 9 answer will be 3 and for 10 answer will be 5
int p = 1;
//Find the power of 2 less than this number
while(true) {
if(p<<1 > num)
break;
p <<= 1;
}
//Now now find the required odd number
cout<<(2*(num-p) + 1)<<endl;
//Take next input
cin>>str;
}
}