第四次CCF-CSP认证
- 第一道(easy)
- 思路及AC代码
- 第二道(easy)
- 思路及AC代码
- 遇到的问题
- 第三道(mid)
- 思路及AC代码
第一道(easy)
题目链接
思路及AC代码
这题就是将这个矩阵旋转之后输出,观察样例,我们可以将这个旋转表示出来,就是从左到右从上到下输出s[j][i],文字版思路:
#include <bits/stdc++.h>
using namespace std;
const int N=1010;
int s[N][N];
int main()
{int n,m;cin>>n>>m;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>s[i][j];}}for(int i=m-1;i>=0;i--){for(int j=0;j<n;j++){cout<<s[j][i]<<" ";}cout<<endl;}
}
第二道(easy)
题目链接
思路及AC代码
#include <bits/stdc++.h>
using namespace std;// 自定义比较函数,用于按出现次数从高到低排序
bool compare(const pair<int, int>& a, const pair<int, int>& b) {if (a.second != b.second) {return a.second > b.second;}// 如果出现次数相同,按数字本身从小到大排序return a.first < b.first;
}int main() {int n;cin >> n;vector<int> s(n);// 用于记录每个数字的出现次数unordered_map<int, int> showup;// 读取输入的数字for (int i = 0; i < n; i++) {cin >> s[i];// 统计每个数字的出现次数showup[s[i]]++;}// 将 unordered_map 中的元素存储到 vector 中vector<pair<int, int>> freq(showup.begin(), showup.end());// 按出现次数从高到低排序sort(freq.begin(), freq.end(), compare);// 输出排序后的结果for (const auto& c : freq) {cout << c.first << " " << c.second << endl;}return 0;
}
遇到的问题
一开始看错题目了 题目是要按出现频率从高到低,我看成按数字本身从大到小输出了,还有一个问题就是当你开了一个大小为1010的静态数组之后,就会发现除去输入的n个数字之后,数组里面默认都是0,当你使用哈希表统计数字和它出现次数之间的映射,输出的时候,就会出现0对应的次数是一个很大的数字所以优化成用vector容器来存储好一些
第三道(mid)
题目链接
思路及AC代码
思路讲解直接去看y总的视频就好(acwing),这是根据代码AI生成的文字版思路,其实最难的就是第三条,我们需要通过题目给的1850年1月1日是星期二来暴力枚举出目标日期是星期几,这个暴力枚举是这题的关键
#include <bits/stdc++.h>
using namespace std;// 判断是否为闰年
bool isLeapYear(int year) {return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}// 获取每个月的天数
int daysInMonth(int year, int month) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return days[month - 1];
}// 计算从 1850 年 1 月 1 日到指定日期的天数
int daysSince1850(int year, int month, int day) {int totalDays = 0;for (int y = 1850; y < year; ++y) {totalDays += isLeapYear(y) ? 366 : 365;}for (int m = 1; m < month; ++m) {totalDays += daysInMonth(year, m);}totalDays += day - 1;return totalDays;
}// 获取指定年份 a 月第 b 个星期 c 的日期
void getDate(int a, int b, int c, int year) {int firstDayOfMonth = (daysSince1850(year, a, 1) + 2) % 7; // 1850 年 1 月 1 日是星期二if (firstDayOfMonth == 0) firstDayOfMonth = 7; // 让星期日对应 7int targetDay = 1 + (c - firstDayOfMonth + 7) % 7 + (b - 1) * 7;if (targetDay > daysInMonth(year, a)) {printf("none\n");} else {printf("%04d/%02d/%02d\n", year, a, targetDay);}
}int main() {int a, b, c, y1, y2;scanf("%d %d %d %d %d", &a, &b, &c, &y1, &y2);for (int year = y1; year <= y2; ++year) {getDate(a, b, c, year);}return 0;
}