L i n k Link Link
l u o g u luogu luogu 1100 1100 1100
D e s c r i p t i o n Description Description
给出一个小于2^32 的正整数。这个数可以用一个32位的二进制数表示(不足32位用0补足)。我们称这个二进制数的前16位为“高位”,后16位为“低位”。将它的高低位交换,我们可以得到一个新的数。试问这个新的数是多少(用十进制表示)。
例如,数1314520用二进制表示为0000 0000 0001 0100 0000 1110 1101 1000(添加了11个前导0补足为32位),其中前16位为高位,即0000 0000 0001 0100;后16位为低位,即0000 1110 1101 1000。将它的高低位进行交换,我们得到了一个新的二进制数0000 1110 1101 1000 0000 0000 0001 0100。它即是十进制的249036820。
I n p u t Input Input
一个小于2^32 的正整数
O u t p u t Output Output
将新的数输出
S a m p l e Sample Sample I n p u t Input Input
1314520
S a m p l e Sample Sample O u t p u t Output Output
249036820
T r a i n Train Train o f of of T h o u g h t Thought Thought
emmmmm一开始做了直接换的做法,然后A了之后
w
h
d
whd
whd巨佬一来,你的代码怎么这么长,直接位运算不就好了吗?
还有那个样例是认真的吗?
C o d e Code Code
手动交换
#include<iostream>
#include<cstdio>
using namespace std;
string s,ss;
long long n,ans;
void work(long long x)
{
string ss1="",s1="";
while (x)
{
s1+=(x%2)+'0';
x=(long long)(x-x%2)>>1;
}
int len=s1.size();
for (int i=len-1; i>-1; --i)
ss1+=s1[i];
for (int i=0; i<32-len; ++i)
s+='0';
s+=ss1;
string s2="",s3="";
for (int i=0; i<16; ++i)
s2+=s[i];
for (int i=16; i<32; ++i)
s3+=s[i];
ss=s3+s2;//交换
for (long long t=0,i=31; i>=0; --i,++t)
{
long long tt=(long long)(ss[i]-'0')<<t;
ans=(long long)ans+tt;
}//计算
}
int main()
{
scanf("%lld",&n);
work(n);
printf("%lld",ans);
}
位运算大法
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
long long n;
scanf("%lld",&n);
long long wjj=n>>16;
long long emm=(n-(n>>16<<16))<<16;
printf("%lld",wjj+emm);
}