2020年十月蓝桥杯A组题解【10月17日】【附完整代码】
填空1
題意:輸出1到2020之中,數字2出現了一共多少次。
題解:有手就行
用時:3min
正確答案:624
代碼:
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);cin.tie(0);int cnt = 0;for (int i = 1; i <= 2020; i++) {int x = i;while (x) {if (x % 10 == 2) ++cnt;x /= 10;}}cout << cnt << "\n";return 0; }填空2
題意:求分子和分母都在1到2020范圍內的分數中,有多少個是既約分數(既約分數定義為分子分母的gcd為1)。
題解:雙重循環硬解, 其中判斷是否為既約分數即可。
用時:40min (別嘲笑我。。)
正確答案:2481215
代碼:
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);cin.tie(0);int cnt = 0;for (int i = 1; i <= 2020; i++) {for (int j = 1; j <= 2020; j++) {if (__gcd(i, j) == 1) ++cnt;}}cout << cnt << "\n";return 0; }填空3
題意:定義了這樣一個蛇形走位的數陣,求第20行20列的數字是多少。
1 3 6
2 5
4
題解:打表、推數學公式、演草紙都可。 最初想用演草紙直接推, 但發現數據量有點大, 就寫了個程序打表做出來了。
用時:20min
正確答案:761
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int x = 1, y = 1;int a[100][100] = {};int num = 1;for (int i = 1; x <= 50; i++) {for (int j = 0; j < i; j++) {a[x][y] = num++;if (j != i - 1) {if (i & 1) --x, ++y;else ++x, --y;}}if (i & 1) ++y;else ++x;}cout << a[20][20] << "\n";return 0; }填空4
題意:給出一個電子計時器那種數字8,在7個筆畫(橫或豎)之間選擇一些點亮,要求點亮的筆畫組成恰好一個連通塊,求方案數
題解:DFS求連通塊
用時:10min(錯了)
正確答案:80
代碼:
#include <bits/stdc++.h> using namespace std;bool light[7]; vector<vector<int> > G(7); int ans;bool judge(vector<int> &v1) {vector<int> v2;queue<int> que;bool vis[7] = {};que.push(v1[0]);vis[v1[0]] = true;while (!que.empty()) {int u = que.front();que.pop();v2.push_back(u);for (int i = 0; i < G[u].size(); i++) {int v = G[u][i];if (find(v1.begin(), v1.end(), v) != v1.end() && !vis[v]) {que.push(v);vis[v] = true;}}}sort(v1.begin(), v1.end());sort(v2.begin(), v2.end());return v2 == v1; }void dfs(int dep) {if (dep == 7) {vector<int> v;for (int i = 0; i < 7; i++) if (light[i]) v.push_back(i);if (v.size() && judge(v)) ++ans; return;}light[dep] = true;dfs(dep + 1);light[dep] = false;dfs(dep + 1); }void build_graph() {G[0].push_back(1), G[0].push_back(5);G[1].push_back(0), G[1].push_back(2), G[1].push_back(6);G[2].push_back(1), G[2].push_back(3), G[2].push_back(6);G[3].push_back(2), G[3].push_back(4);G[4].push_back(3), G[4].push_back(5), G[4].push_back(6);G[5].push_back(0), G[5].push_back(4), G[5].push_back(6);G[6].push_back(1), G[6].push_back(2), G[6].push_back(4), G[6].push_back(5); }int main() {ios::sync_with_stdio(false);cin.tie(0);build_graph();dfs(0);cout << ans << "\n";return 0; }填空5
題意:問20條直線和20個圓最多能分割多少個平面
題解:直接套直線和圓的分割平面公式即可(后悔ing。。。)
正確答案:1391
代碼:
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);cin.tie(0);int line[25] = {};line[1] = 2;for (int i = 2; i <= 20; i++) {line[i] = line[i - 1] + i;}int ans = line[20];for (int i = 1; i <= 20; i++) {ans += 40 + 2 * (i - 1);}cout << ans << "\n";return 0; }大題一
題意:給出n個學生的成績,分別輸出最高分、最低分、成績均值(保留兩位);
題解:有手就行
用時:5min
代碼:
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);cin.tie(0);int n;scanf("%d", &n);int mx = -1, mi = 101, sum = 0;for (int i = 0; i < n; i++) {int x;scanf("%d", &x);mx = max(mx, x);mi = min(mi, x);sum += x;}printf("%d\n", mx);printf("%d\n", mi);printf("%.2f\n", 1.0 * sum / n);return 0; }大題2
題意:首先介紹了八位數字表示日期,如2020年2月2日表示為20200202,現在給出一個八位數字表示的日期,保證合法且年份在1000至8999之間,分別求該日期之后的第一個滿足以下條件的日期的八位表示并輸出(分別輸出兩個):
1、八位表示下是回文串的日期;
2、八位表示下是ABABBABA的日期。
題解:一天一天加,每加一次都判斷年月日是否符合規定(算閏年之類的), 轉化成字符串后,再判斷是否為回文。 洋洋灑灑寫了一百多行。
老老實實寫模擬準沒錯。
代碼:
#include <bits/stdc++.h> using namespace std; const int month[2][13] = {{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; string my_to_string(int n) {string s;do {s += '0' + n % 10;n /= 10;} while (n);reverse(s.begin(), s.end());return s; } int my_stoi(const string &s) {int res = 0;for (int i = 0; i < int(s.size()); i++) {res = res * 10 + s[i] - '0';}return res; } bool is(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } bool legal(const string &s) {int y = my_stoi(s.substr(0, 4));int m = my_stoi(s.substr(4, 2));int d = my_stoi(s.substr(6, 2));if (m < 1 || m > 12) return false;if (d < 1 || d > month[is(y)][m]) return false;return true; } int main() {ios::sync_with_stdio(false);cin.tie(0);vector<int> v1, v2;for (int i = 1000; i <= 9999; i++) {string s1(my_to_string(i));string s2(s1.rbegin(), s1.rend());string s = s1 + s2;string t(s.rbegin(), s.rend());if (legal(s)) {if (s == t) {v1.push_back(my_stoi(s));}if ((s[0] == s[2] && s[2] == s[5] && s[5] == s[7]) && (s[1] == s[3] && s[3] == s[4] && s[4] == s[6]) && (s[0] != s[1])) {v2.push_back(my_stoi(s));}}}int n;cin >> n;cout << *upper_bound(v1.begin(), v1.end(), n) << "\n";cout << *upper_bound(v2.begin(), v2.end(), n) << "\n";return 0; }大題三
題意:對于字符串s,定義一個f(s)表示該串中出現恰好一次的字母的個數,現在給出一個字符串S,串長105,求其所有子串t的Σf(t)。
樣例:ababc輸出21。
題解:當初直接用三重循環暴力解的, 下面的代碼是滿分答案
代碼:
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);cin.tie(0);string s;cin >> s;int n = s.size();vector<vector<int> > pos(26);for (int i = 0; i < 26; i++) {pos[i].push_back(-1);}for (int i = 0; i < n; i++) {pos[s[i] - 'a'].push_back(i);}for (int i = 0; i < 26; i++) {pos[i].push_back(n);}long long ans = 0;for (int i = 0; i < 26; i++) {for (int j = 1; j + 1 < int(pos[i].size()); j++) {int l = pos[i][j] - pos[i][j - 1] - 1;int r = pos[i][j + 1] - pos[i][j] - 1;ans += 1LL * (l + 1) * (r + 1);}}cout << ans << "\n";return 0; }只更到這里吧, 部分代碼憑借記憶敲得, 部分代碼參考了網上的思路。 這些都做出來, 省一一定是穩穩地了, 至于大題4和5, 有思路會后續更新的, 請持續關注~
總結
以上是生活随笔為你收集整理的2020年十月蓝桥杯A组题解【10月17日】【附完整代码】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【已解决】报错:cannot be re
- 下一篇: 报错:out cannot be res