题目链接:点击打开链接
A: 两个人在两端相向而行, 第一次碰面后掉头, 到端点后又掉头, 问第二次碰面时候距离左端点的距离.
脑洞题目, 第二次和第一次碰面在同一个地方, 要求精度在1e-4以内, 输出5位小数即可.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
int l, p, q;
int main(int argc, char const *argv[])
{
scanf("%d%d%d", &l, &p, &q);
printf("%.5f\n", (1.0 * l * p) / (p + q));
return 0;
}
模拟题目, 定义一个存储26个字母的数组, 每次操作对数组进行操作即可, 输出的时候运用该数组输出.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 2e5 + 5;
int n, m;
char s[MAXN], word[30];
int main(int argc, char const *argv[])
{
cin >> n >> m >> s;
for(int i = 0; i < 26; ++i)
word[i] = i + 'a';
for(int i = 0; i < m; ++i) {
char x, y;
cin >> x >> y;
for(int j = 0; j < 26; ++j)
if(word[j] == x) word[j] = y;
else if(word[j] == y) word[j] = x;
}
for(int i = 0; i < n; ++i)
cout << word[s[i] - 'a'];
cout << endl;
return 0;
}
a[i + 1]的中位数.
模拟题. 列举可能出现的情况, 可以发现101, 010会发生变化. 定义一个dp数组求出经过变换的次数, 然后输出变换后的数组即可.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 5e5 + 5;
int n, ans, a[MAXN], dp[MAXN];
int main(int argc, char const *argv[])
{
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
for(int i = 1; i < n; ++i)
if(a[i] != a[i - 1]) dp[i] = dp[i - 1] + 1;
else dp[i] = 0;
dp[n - 1] = 0;
for(int i = n - 1; i > 0; --i)
if(a[i] != a[i + 1]) dp[i] = min(dp[i], dp[i + 1] + 1);
else dp[i] = 0;
for(int i = 0; i < n; ++i)
ans = max(ans, dp[i]);
printf("%d\n", ans);
for(int i = 0; i < n; ++i)
if(i == 0 || i == n - 1) printf("%d ", a[i]);
else printf("%d ", a[i] ^ (dp[i] & 1));
printf("\n");
return 0;
}