题意:输出32^(a-b)即可。这里敲了个快速幂。
#include<iostream>
using namespace std;
typedef long long ll;
ll q_pow(int a, int b)
{
ll ans = 1;
while (b)
{
if (b & 1) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
int main()
{
ll a, b;
cin >> a >> b;
cout << q_pow(32, a - b);
return 0;
}
题意:能否通过交换相邻的两位使S串等于T串。
思路:数据范围较小,暴力就完了,复杂度o(n^2)。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s, t;
cin >> s >> t;
if (s == t) { //必不可少的特判
cout << "Yes";
return 0;
}
for (int i = 0; i <= s.length() - 1; i++)
{
swap(s[i], s[i + 1]); //交换相邻两位
if (s == t) {
cout << "Yes";
return 0;
}
swap(s[i], s[i + 1]); //还原相邻两位
}
cout << "No";
return 0;
}
题意:给定一个整数,你可以分离数位构成两个数。问:两个数乘积最大能有多大?
思路:两个数大且接近时乘积最大。
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
string s;
cin >> s;
sort(s.begin(), s.end(), [](const char& ch1, const char ch2) {
return ch1 > ch2;
}); //倒叙排列
int a = 0, b = 0;
for (int i = 0; i < s.length(); i++)
{
int t = s[i] - '0';
//动态调整两数(大且接近)
if (a > b) {
b = b * 10 + t;
}
else
a = a * 10 + t;
}
cout << a * b;
return 0;
}
题意:给定一个一个人的上线时间,以及上线天数。问:同时有1-n个人在线的天数位?
思路:类似差分。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 2e5 + 5;
int n;
int main()
{
cin >> n;
vector<pair<int, int> >a;
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
a.emplace_back(x, 1);
a.emplace_back(x + y, -1);
}
sort(a.begin(), a.end());//排序排出先后顺序
vector<int>ans(N, 0);
int cnt = 0;
//计算时间段内的人数
for (int i = 0; i < a.size() - 1; i++)
{
cnt += a[i].second;
ans[cnt] += (a[i + 1].first - a[i].first);
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << ' ';
}
return 0;
}