16 条题解
-
-1
1.int型最多存储31位整形[即正负数都有,范围在- − -1],而unsigned int也是31位,但是存储的是正整数[范围是 − -1]。
2.C++运算中,若出现溢出,则 自动取模 。举个例子:在unsigned int型当中,计算+400时,实际的运算是(+400)%,结果就是400.
综合以上几点,能不能发现什么呢?
问题让我们求的其实是2进制下交换前后16位,不就是向左移16位(根据上文2.),加上右移16位吗?
代码如下
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int main(){ unsigned int n; cin>>n; cout<< (n<<16)+(n>>16); return 0; }
-
-1
#include<iostream> #include<cmath> using namespace std; string t00(int num){ string s = ""; while(num){ s += char(num%2+'0'); num /= 2; } for(int i = 0; i < s.size() / 2; i++){ swap(s[i],s[s.size()-i-1]); } if(s.size() < 32){ while(s.size() != 32){ s = '0' + s; } } return s; } string sw(string s){ for(int i = 0; i < 16; i++){ swap(s[i],s[i+16]); } return s; } long long t01(string s){ long long ans = 0; for(int i = 31; i >= 0; i--){ ans += (long long)((s[i]-'0') * pow(2,31-i)); } return ans; } void run(){ int n; cin >> n; cout << t01(sw(t00(n))); } int main(){ run(); }
-
-2
#include <queue> #include <math.h> #include <stack> #include <stdio.h> #include <iostream> #include <vector> #include <iomanip> #include <string.h> #include <algorithm> using namespace std; #define LL long long const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; int main() { // unsigned int a,b; // cin >> a; // unsigned int c=a>>16; // b=a-(c<<16); // unsigned int x=(b<<16)+c; // cout << x <<endl; unsigned int a; cin >> a; cout << (a << 16)+(a >> 16); }
- 1
信息
- ID
- 1219
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 4
- 标签
- 递交数
- 516
- 已通过
- 238
- 上传者