CSDN周赛第58期
T1
思路:很经典的动态规划问题,枚举每一个房间,根据每一个房间选与不选来写状态计算式子
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
int a[N], f[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) {
f[i] = a[i];
f[i] = max(f[i - 1], f[i]);
if(i >= 2) f[i] = max(f[i - 2] + a[i], f[i]);
}
cout << f[n] << "\n";
return 0;
}
T2
思路:根据题意模拟即可
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, l, c, d, p, nl, np;
cin >> n >> k >> l >> c >> d >> p >> nl >> np;
int x = (k * l) / nl;
int y = c * d;
int z = p / np;
cout << min( {
x / n, y / n, z / n
}
) << "\n";
return 0;
}
T3
思路:根据题意将符合要求第一个的at字符串替换为@,以及所有符合要求的dot替换为.即可
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
string ans;
bool is_at = false;
for (int i = 0; i < n;) {
if(i + 2 < n && i > 0 && i + 2 != n - 1 && s[i] == 'd' && s[i + 1] == 'o' && s[i + 2] == 't') {
ans += '.';
i += 3;
continue;
}
if(i + 1 < n && i > 0 && i + 1 != n - 1 && s[i] == 'a' && s[i + 1] == 't' && !is_at) {
ans += '@';
i += 2;
is_at = true;
continue;
}
ans += s[i ++ ];
}
cout << ans << "\n";
return 0;
}
T4
思路:
根据题目的要求,我们只需要关心最后一个输入的字符是什么。根据输入字符串的首尾字符和它们是否为元音字母,我们可以得出以下情况:
-
如果输入字符串的首尾字符都是元音字母,那么最后一个输入的字符一定是输入字符串的首字符。
-
如果输入字符串的首尾字符都不是元音字母,那么最后一个输入的字符就是输入字符串的末字符。
-
如果输入字符串的首字符是元音字母,而末字符不是元音字母,那么最后一个输入的字符可能是输入字符串的首字符或末字符。
综上所述,我们只需要判断输入字符串的首尾字符是否为元音字母即可确定最后一个输入的字符的可能性。
#include <iostream>
#include <string>
using namespace std;
void solution(const string& str) {
int length = str.length();
int pos[length];
int index = 0;
for (int i = 0; i < length; i++) {
char s = str[i];
if (s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u') {
pos[index] = i;
index++;
}
}
length = index;
if (length == 0) {
cout << 1 << endl;
} else if (pos[0] != 0) {
cout << 0 << endl;
} else {
if (length == 1) {
cout << (int)str.length() - pos[0] << endl;
} else if (length % 2 == 1) {
cout << pos[length / 2 + 1] - pos[length / 2] << endl;
} else {
cout << pos[length / 2] - pos[length / 2 - 1] << endl;
}
}
}
int main() {
string str;
cin >> str;
solution(str);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!