记录历年CCF CSP中的中档题(第三题)
201503-3
节日
#include <iostream> using namespace std; const int N = 1e6 + 10; int a, b, c, y1, y2; int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int rec[3000][15][40]; bool is_leap(int x) { if ((x % 4 == 0 && x % 100) || x % 400 == 0) return true; return false; } int main() { cin >> a >> b >> c >> y1 >> y2; int ini = 2; for (int i = 1850; i <= 2050; i ++ ) { for (int j = 1; j <= 12; j ++ ) { int days = month[j]; if (is_leap(i) && j == 2) days ++; for (int k = 1; k <= days; k ++ ) { rec[i][j][k] = ini ++ % 7; } } } for (int i = y1; i <= y2; i ++ ) { bool flag = false; if (c == 7) c = 0; int days = month[a]; if (is_leap(i) && a == 2) days ++; int cnt = 0; for (int k = 1; k <= days; k ++ ) { if (rec[i][a][k] == c) cnt ++; if (cnt == b) { printf("%d/%02d/%02d\n", i, a, k); flag = true; break; } } if (!flag) puts("none"); } return 0; }

201412-3
集合竞价
#include <iostream> using namespace std; const int N = 1e6 + 10; int n, mx, tmp, b[N]; double a[N]; string op[N]; long long res, by[N], sl[N]; int main() { for (int i = 1; ; i ++ ) { if (cin >> op[i]) n ++; else break; if (op[i][0] != 'c') cin >> a[i] >> b[i]; else { cin >> tmp; op[tmp] = 'd'; } } for (int i = 1; i <= n; i ++ ) { tmp = a[i] * 1000 / 10; if (op[i][0] == 'b') by[tmp] += b[i]; if (op[i][0] == 's') sl[tmp] += b[i]; } for (int i = 1; i <= N; i ++ ) by[i] += by[i - 1], sl[i] += sl[i - 1]; for (int i = 1; i <= N; i ++ ) if (min(sl[i], by[N] - by[i - 1]) >= min(sl[mx], by[N] - by[mx - 1])) mx = i; printf("%.2lf %lld\n", mx / 100.0, min(sl[mx], by[N] - by[mx - 1])); return 0; }

201409-3
字符串匹配
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 1010; int n, st; string S, str[N]; void lw(string& s) { for (int i = 0; i < s.size(); i ++ ) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a'; } bool find(string s) { if (!st) lw(s); if (!st) lw(S); for (int i = 0; i < s.size(); i ++ ) if (S == s.substr(i, S.size())) return true; return false; } int main() { cin >> S >> st >> n; for (int i = 1; i <= n; i ++ ) cin >> str[i]; for (int i = 1; i <= n; i ++ ) if (find(str[i])) cout << str[i] << endl; return 0; }

201403-3
命令行选项
#include <bits/stdc++.h> using namespace std; unordered_map<char, int> h; unordered_map<char, int> uh; int ck(string s) { return s[0] == '-' && s.size() == 2; } bool find(string s, vector<string> &res) { for (auto& x : res) if (x.substr(0, 2) == s.substr(0, 2)) return true; return false; } void rpc(string s, vector<string> &res) { for (auto& x : res) if (x.substr(0, 2) == s.substr(0, 2)) x = s; } bool cmp(string a, string b) { return a[1] < b[1]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; getline(cin, s); for (int i = 0; i < s.size(); i ++) { if (i + 1 < s.size() && s[i + 1] == ':') { h[s[i]] = 1; i ++; } else uh[s[i]] = 1; } getline(cin, s); int n = atoi(s.c_str()); for (int i = 1; i <= n; i ++) { vector<string> now, res; int st = 0; string t; getline(cin, s); for (int j = 0; j < s.size(); j ++) { if (s[j] == ' ') { st ++; if (t.size()) now.push_back(t); t.clear(); } else if (st) t += s[j]; } if (t.size()) now.push_back(t); t.clear(); for (int j = 0; j < now.size(); j ++) { if (ck(now[j])) if (h[now[j][1]] && j + 1 < now.size() ) { if (find(now[j], res)) rpc(now[j] + ' ' + now[j + 1], res); else res.push_back(now[j] + ' ' + now [j + 1]); j ++; } else if (uh[now[j][1]] && !find(now[j], res)) res.push_back(now[j]); else break; else break; } sort(res.begin(), res.end(), cmp); cout << "Case " << i << ": "; for (auto x : res) cout << x << ' '; cout << endl; } return 0; }

201312-3
最大的矩形
#include <iostream> using namespace std; const int N = 1010; int n, res; int q[N]; int main() { cin >> n; for (int i = 1; i <= n; i ++ ) cin >> q[i]; for (int i = 1; i <= n; i ++ ) { int cnt = 1, j = i; while (j - 1 >= 1 && q[j - 1] >= q[i]) j --; cnt += i - j; j = i; while (j + 1 <= n && q[j + 1] >= q[i]) j ++; cnt += j - i; res = max(res, q[i] * cnt); } cout << res; return 0; }


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号