1、劲舞团
小蓝最近迷上了一款名为 “劲舞团” 的游戏,具体来说,只要按照游戏中给出的键位提示依次按出对应的键位,游戏人物便可以跟随节奏跳舞。对于连续的 K 次正确敲击,如果任意连续的两次敲击间间隔时间都小于等于 1s,那么我们称这是一次 K 连击。现在给出一局小蓝的游戏记录文件,log.txt 中记录了 N 条记录,每条记录有三个字段,依次为正确的敲击字符、小蓝打出的字符、 打出字符的时间对应的毫秒时间戳。现在请你计算下最长的 K 连击是多少,你只需要输出 K 的值。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Record {
char correctKey; // 正确的键
char actualKey; // 实际按下的键
long long timestamp; // 时间戳(毫秒)
};
int main() {
ifstream ifs("C:/Users/13684/Desktop/log.txt", ios::in);
if (!ifs.is_open()) {
cout << "read fail." << endl;
return -1;
}
vector<Record> records;
Record rec;
while (ifs >> rec.correctKey >> rec.actualKey >> rec.timestamp) {
records.push_back(rec);
}
ifs.close();
int maxCombo = 0; // 最长连击次数
int currentCombo = 0; // 当前连击次数
long long lastTimestamp = -1; // 上一次正确敲击的时间戳
for (size_t i = 0; i < records.size(); ++i) {
if (records[i].correctKey == records[i].actualKey) {
if (lastTimestamp == -1 || records[i].timestamp - lastTimestamp <= 1000) {
currentCombo++;
}
else {
currentCombo = 1;
}
lastTimestamp = records[i].timestamp;
}
else {
currentCombo = 0;
lastTimestamp = -1;
}
maxCombo = max(maxCombo, currentCombo);
}
cout << "最长的 K 连击是: " << maxCombo << endl;
return 0;
}
比赛时其实不用对文件操作,题目会输入数据。可以不用读文件和vector的操作,直接对输入进行判断:
#include <iostream>
using namespace std;
int main()
{
struct Record{
char correct;
char userdo;
long long dotime;
};
Record r;
int Maxhit=0;int currenthit=0;long long predotime=0;
while(cin>>r.correct>>r.userdo>>r.dotime){
if(r.correct==r.userdo){
if(r.dotime-predotime<=1000)
currenthit++;
else
currenthit=1;
}else
currenthit=0;
predotime=r.dotime;
if(Maxhit<currenthit)
Maxhit=currenthit;
}
cout<<Maxhit;
return 0;
}
2、召唤数学精灵
数学家们发现了两种用于召唤强大的数学精灵的仪式,这两种仪式分别被称为累加法仪式 A(n) 和累乘法仪式 B(n)。
累加法仪式 A(n) 是将从 1 到 n 的所有数字进行累加求和,即:A(n)=1+2+⋯+n 累乘法仪式 B(n) 则是将从 1 到 n 的所有数字进行累乘求积,即:B(n)=1×2×⋯×n 据说,当某个数字 i 满足 A(i)−B(i) 能被 100 整除时,数学精灵就会被召唤出来。
现在,请你寻找在 1 到 2024041331404202 之间有多少个数字 i,能够成功召唤出强大的数学精灵。
#include <iostream>
using namespace std;
int main() {
long long N = 2024041331404202LL;
// 计算区间内有多少个完整的200
long long complete200s = N / 200;
long long res = complete200s * 4; // 每个200区间有4个符合条件的i
// 处理剩余的部分 [1, 9]
int s1 = 0, s2 = 1;
for (int i = 1; i <= 9; ++i) {
s1 += i;
s2 *= i;
if (s1 % 100 == s2 % 100) {
res += 1;
}
}
// 输出结果
cout << res << endl;
return 0;
}
参考: 第十五届蓝桥杯省赛PythonA组B题【召唤数学精灵】题解,比较巧妙,需要先观察规律。
3、封闭图形个数
在蓝桥王国,数字的大小不仅仅取决于它们的数值大小,还取决于它们所形成的“封闭图形”的个数。
封闭图形是指数字中完全封闭的空间,例如数字 1、2、3、5、7 都没有形成封闭图形,而数字 0、4、6、9 分别形成了 1 个封闭图形,数字 8 则形成了 2 个封闭图形。值得注意的是,封闭图形的个数是可以累加的。例如,对于数字 68,由于 6 形成了 1 个封闭图形,而 8 形成了 2 个,所以 68 形成的封闭图形的个数总共为 3。
在比较两个数的大小时,如果它们的封闭图形个数不同,那么封闭图形个数较多的数更大。例如,数字 41 和数字 18,它们对应的封闭图形的个数分别为 1 和 2,因此数字 41 小于数字 18。如果两个数的封闭图形个数相同,那么数值较大的数更大。例如,数字 14 和数字 41,它们的封闭图形的个数都是 1,但 14<41,所以数字 14 小于数字 41。 如果两个数字的封闭图形个数和数值都相同,那么这两个数字被认为是相等的。
小蓝对蓝桥王国的数字大小规则十分感兴趣。现在,他将给定你 nn 个数 a1,a2,…,an,请你按照蓝桥王国的数字大小规则,将这 n 数从小到大排序,并输出排序后结果。
输入格式
第一行包含一个整数 n,表示给定的数字个数。
第二行包含 n 个整数 a1,a2,…,an,表示待排序的数字。
输出格式
输出一行,包含 n 个整数,表示按照蓝桥王国的数字大小规则从小到大排序后的结果,每两个数字之间用一个空格分隔。
#include <iostream>
#include <vector>
#include <algorithm> // 引入算法库
using namespace std;
int main() {
struct s {
int num;
int record;
};
int n;
cin >> n;
vector<s> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i].num;
int t = nums[i].num;
nums[i].record = 0; // 初始化 record 为 0
while (t > 0) { // 确保处理所有位
int a = t % 10;
if (a == 0 || a == 4 || a == 6 || a == 9) nums[i].record++;
else if (a == 8) nums[i].record += 2;
t /= 10;
}
}
// 使用 std::sort 进行排序
sort(nums.begin(), nums.end(), [](const s &a, const s &b) {
if (a.record == b.record) return a.num < b.num; // 如果 record 相同,按 num 升序
return a.record < b.record; // 否则按 record 升序
});
// 输出结果
for (int i = 0; i < n; i++) {
cout << nums[i].num << " ";
}
return 0;
}
思路比较简单。需要注意的是如果排序用n方的方法只能通过一半用例,需要更高效的排序方法。