-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path二进制强化.cpp
68 lines (66 loc) · 997 Bytes
/
二进制强化.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
#include<iostream>
#include<bitset>
#include<string>
typedef long long ll;
using namespace std;
ll ksm(ll a,ll b)
{
ll r=1,base=a;
while(b)
{
if(b&1) r*=base; //奇数
base*=base;
b>>=1; //右移1位
}
return r;
}
string dtob(ll n)
{
string s="";
char c;
int end;
while(n) //当n不为0
{
end=n&1; //n%2
c=end+'0';
s=c+s;
n>>=1; //n=n/2
}
return s;
}
string strrev(string str) //字符串反转函数
{
string s="";
int i,l=str.size();
for(i=0;i<l;i++)
s+=str[l-1-i];
return s;
}
ll btol(string str)
{
int l=str.size(),i,k;
ll r=0;
//从高位往低位算
for(i=0;i<l;i++)
{
k=str[i]-'0';
if(k==0)
continue;
r+=ksm(2,l-1-i);
}
return r;
}
int main()
{
//北邮18机试题
//输入数据0-2的32次方范围,化成二进制,然后逆序这个二进制序列,转换成十进制
ll n,ans;
string t,s;
while(cin>>n)
{
t=dtob(n);
s=strrev(t); //反转二进制字符串
ans=btol(s);
cout<<ans<<endl;
}
}