7.25综合测试总结
T637642 比较数字
题目描述
小梦在学习比较数字,然后想到一个这样的问题,如果数字
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
string a; cin >> a;
int len = a.size();
for (int i = 0; i < len; i++) {
if (a[i] == '0') cout << 1;
else cout << a[i];
}
return 0;
}
//12340
//12301000
错误原因
思考不全面
正确思路
- 我们可以自己早一个样例
比如:
按照原来的数量会变成
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
string a; cin >> a;
int len = a.size();
bool flag = false;
for (int i = 0; i < len; i++) {
if (flag == true) cout << 1;
else if (a[i] == '0') {cout << 1; flag = true;}
else cout << a[i];
}
return 0;
}
//12340
//12301000
T637627 张的弓
题目描述
在
上图中,从
曼哈顿距离定义:沿直角坐标轴测量的两点之间的距离。在平面上,若
输入格式
输入第一行包含一个正整数
输出格式
输出对于每组测试数据,一行一个整数,表示
说明/提示
数据范围
对于
对于
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
m--; n--; int x, y;
int s = k / (n + 1);
int sum = k % (n + 1);
if (sum != 0) {
if (s % 2 == 1) {
if (sum == n + 1) {
x = s, y = n;
}
else {
x = s + 1, y = n + 1 - sum;
}
}
else {
if (sum == n + 1) {
x = s, y = 1;
}
else {
x = s + 1, y = 1 + sum;
}
}
}
else {
if (s % 2 == 1) x = s + 1, y = n + 1;
else x = s + 1, y = 1;
}
// cout << s << " " << sum << endl;
// cout << x << " " << y << endl;
cout << abs(x - 1) + abs(y - 1) << endl;
}
return 0;
}
/*
5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5
*/
T637602 可可口乐
题目描述
收完玉米之后,小
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
string st, ed = "cocacola";
int ans;
map<string, bool> vis;
struct node {
string ans;
int step;
};
void bfs(string a) {
queue<node> q;
q.push({a, 0});
vis[a] = true;
while (!q.empty()) {
node cur = q.front();
q.pop();
if (cur.ans == ed) {
cout << cur.step; return;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i == j) continue;
string ns = cur.ans;
swap(ns[i], ns[j]);
if (vis[ns] == false) {
q.push({ns, cur.step + 1});
vis[ns] = true;
}
}
}
}
cout << -1;
return;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> st;
if (st == ed) {
cout << 0; return 0;
}
bfs(st);
return 0;
}
//cococala
//locccaoa
T637561 小梦的守卫
题目描述
小梦所在的城市遭受了僵尸入侵!
为了守护自己的家园,小梦立马派遣了贝家军赶往一线战场抵御僵尸的攻击。然而,僵尸的数量越来越多,小梦的守卫数量与体力有限。在大部队赶到之前,小梦需要实时计算贝家军的伤亡情况。
小梦的贝家军由
例如,贝家军有
-
\{1,3,2\}\stackrel{1秒后}{\longrightarrow}\{3,1\}\stackrel{2秒后}{\longrightarrow}\{2\}\stackrel{3秒后}{\longrightarrow}\{1\}\stackrel{4秒后}{\longrightarrow}\{\}
小梦会给出
注意:当队伍中只剩下一名士兵时,该士兵每秒消耗的体力值依旧为
输入格式
第一行
第二行是长度为
接下来
输出格式
对于每个询问输出一行内容。
若士兵数量大于
错误代码
无
错误原因
想出正解了, 未在规定时间内写完
正确思路
样例:
-
我们可以先求一遍前缀和, 在求一遍后缀和
-
我们可以求出:
前缀和数组中第一个
>t 的位置再-1 , 也就是左边倒下的人, 记为L ;后缀和数组中第一个
>t 的位置再-1 ,也就是右边倒下的人, 记为R 。 -
如果
L + R > n , 输出"Zombies ate your brain!" 否则, 输出n - l - r
-
我们可以推一下样例的第一次询问:
那么先推一下样例的前缀和与后缀和:
原数组:
1 2 2 1 5 前缀和:
1 3 5 6 11 后缀和:
11 10 8 6 5 当
t = 3 时:前缀和数组中第一个
>3 的位置下标为3 , 因此L = 3 - 1 = 2 ;后缀和数组中第一个
>3 的位置下标为1 , 因此R = 1 - 1 = 0 ;由于
2 + 0 = 2 ,2 < 5 , 所以答案是5 - 2 = 3 , 答案正确因此我们的思路是对的 -
所以核心代码长这样:
int pos1 = upper_bound(sum1 + 1, sum1 + 1 + n, t) - sum1 - 1;
int pos2 = upper_bound(sum2 + 1, sum2 + 1 + n, t) - sum2 - 1;
if (pos1 + pos2 > n) cout << "Zombies ate your brain!\n";
else cout << n - pos1 - pos2 << endl;
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, q;
int a[1000007];
int sum1[1000007];
int sum2[1000007];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) sum1[i] = sum1[i - 1] + a[i];
for (int i = n; i >= 1; i--) sum2[n - i + 1] = sum2[n - i] + a[i];
while (q--) {
int t;
cin >> t;
int pos1 = upper_bound(sum1 + 1, sum1 + 1 + n, t) - sum1 - 1;
int pos2 = upper_bound(sum2 + 1, sum2 + 1 + n, t) - sum2 - 1;
if (pos1 + pos2 > n) cout << "Zombies ate your brain!\n";
else cout << n - pos1 - pos2 << endl;
}
return 0;
}
//locccaoa