日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

第十一届蓝桥杯大赛软件赛省赛第二场 C/C++ 大学B组

發(fā)布時間:2025/3/19 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第十一届蓝桥杯大赛软件赛省赛第二场 C/C++ 大学B组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

試題A :門牌制作

// 624 #include <iostream> using namespace std;int solve(int x) {int res = 0;while (x){if (x % 10 == 2) res ++ ;x /= 10;}return res; }int main() {int res = 0;for (int i = 1; i <= 2020; i ++ ){res += solve(i);}cout << res; }

試題B :既約分數(shù)

// 2481215 #include <iostream> using namespace std;int gcd(int a, int b) {return b ? gcd(b, a % b) : a; }int main() {int res = 0;for (int fenzi = 1; fenzi <= 2020; fenzi ++ )for (int fenmu = 1; fenmu <= 2020; fenmu ++ ){if (gcd(fenzi, fenmu) == 1)res ++ ;}cout << res; }

試題C :蛇形填數(shù)

// 761 #include <iostream> using namespace std;int cnt = 1; int g[100][100];int main() {int x = 1, y = 1;while (cnt <= 3000){// 1g[x][y] = cnt ++ ;// 2y ++ ;for ( ; y >= 1; x ++ , y -- )g[x][y] = cnt ++ ;x -- , y ++ ;// 3x ++ ;for ( ; x >= 2; x -- , y ++ )g[x][y] = cnt ++ ;}cout << g[20][20]; }

試題D :跑步鍛煉

// 8879 #include <iostream> using namespace std;int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int get_day(int year, int month) {if (month != 2) return months[month];if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) return 29;return 28; }int main() {int sum = 0, res = 0;for (int i = 2000; i <= 2019; i ++ ){for (int j = 1; j <= 12; j ++ ){for (int k = 1; k <= get_day(i, j); k ++ ){int weekend = (sum % 7 + 5) % 7;if (k == 1 || weekend == 0) res += 2;else res ++ ;sum ++ ;}}}for (int i = 1; i <= 9; i ++ ){for (int j = 1; j <= get_day(2020, i); j ++ ){int weekend = (sum % 7 + 5) % 7;if (j == 1 || weekend == 0) res += 2;else res ++ ;sum ++ ;}}res += 2;cout << res; }

試題E :七段碼

  • dfs+并查集
// 80 #include <iostream> using namespace std;int res; int e[10][10]; bool vis[10]; int p[10];int find(int x) {if (p[x] != x) p[x] = find(p[x]);return p[x]; }bool check() {for (int i = 0; i < 10; i ++ ) p[i] = i;for (int i = 0; i < 10; i ++ )for (int j = 0; j < 10; j ++ )if (vis[i] && vis[j] && e[i][j]){int ii = find(i), jj = find(j);p[ii] = jj;}int cnt = 0;for (int i = 0; i <= 6; i ++ )if (vis[i] && p[i] == i)cnt ++ ;if (cnt != 1) return false;return true; }void dfs(int u) {if (u == 7){if (check()) res ++ ;return ;}vis[u] = true;dfs(u + 1);vis[u] = false;dfs(u + 1); }int main() {e[0][1] = e[1][0] = true;e[0][3] = e[3][0] = true;e[0][4] = e[4][0] = true;e[1][2] = e[2][1] = true;e[2][3] = e[3][2] = true;e[2][6] = e[6][2] = true;e[3][4] = e[4][3] = true;e[3][6] = e[6][3] = true;e[4][5] = e[5][4] = true;e[5][6] = e[6][5] = true;dfs(0);cout << res; }

試題F :成績統(tǒng)計

#include <iostream> using namespace std;int main() {int n; cin >> n;int jige = 0, youxiu = 0;for (int i = 0; i < n; i ++ ){int grade; cin >> grade;if (grade >= 60) jige ++ ;if (grade >= 85) youxiu ++ ;}cout << (int)(100.0 * jige / n + 0.5) << '%' << endl;cout << (int)(100.0 * youxiu / n + 0.5) << '%' << endl; }

試題G :回文日期

  • 直接枚舉回文數(shù),再來判斷合法性
#include <iostream> #include <algorithm> using namespace std;int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int get_day(int yy, int mm) {if (mm != 2) return months[mm];if ((yy % 400 == 0) || (yy % 4 == 0 && yy % 100 != 0)) return 29;return 28; }int main() {int n; cin >> n;bool ok1 = false, ok2 = false;string ans1, ans2;for (int year = n / 10000; ; year ++ ){string a = to_string(year);string b = a;reverse(b.begin(), b.end());if (a + b <= to_string(n)) continue;int month = stoi(b.substr(0, 2));int day = stoi(b.substr(2, 2));if (month < 1 || month > 12) continue;if (day < 1 || day > get_day(year, month)) continue;if (!ok1) ans1 = a + b, ok1 = true;if (!ok2){if (year / 100 == year % 100)ans2 = a + b, ok2 = true;}if (ok1 && ok2) break;}cout << ans1 << endl << ans2; }

試題H :子串分值和

  • 算每一個位置的貢獻
  • 因為是算每個連續(xù)子串中不同字符的個數(shù),因此 對于原字符串中所有相同的字符而言,每一個字符做出的貢獻中,當前子串必然不包含上一次這個字符出現(xiàn)的位置及以前,包含這個字符出現(xiàn)的位置至原字符串末尾
#include <iostream> using namespace std;typedef long long ll;int last[1000];int main() {string s; cin >> s;int n = (int)s.size();s = "0" + s;ll res = 0;for (int i = 1; i <= n; i ++ ){res += (ll)(i - last[s[i] - 'a']) * (n - i + 1);last[s[i] - 'a'] = i;}cout << res; }

試題I :平面切分

  • 每增加一條直線,它對答案的貢獻是 它與之前直線的所有交點(要去重)個數(shù)+1
  • 直線本身也需要去重
  • 因此,兩次用到set
  • 計算交點的時候,注意分母為0情況
  • 易錯點 :涉及直線或點,經(jīng)常要用到set容器來去重,且每次計算分式必然要討論分母為零的情況
#include <iostream> #include <set> #include <vector> using namespace std;typedef pair<int, int> PII;set<PII> se1; vector<PII> linear;int main() {int n; cin >> n;for (int i = 0, a, b; i < n && scanf("%d%d", &a, &b); i ++ )se1.insert({a, b});for (auto pii : se1)linear.push_back(pii);int res = 2;for (int i = 1; i < linear.size(); i ++ ){set<pair<long double, long double>> point;for (int j = 0; j < i; j ++ ){int a1 = linear[i].first, b1 = linear[i].second;int a2 = linear[j].first, b2 = linear[j].second;if (a1 == a2) continue;long double x = (long double)(b2 - b1) / (a1 - a2);long double y = (long double)(a1 * b2 - a2 * b1) / (a1 - a2);point.insert({x, y});}res += point.size() + 1;}printf("%d", res); }

試題J :字串排序

總結(jié)

以上是生活随笔為你收集整理的第十一届蓝桥杯大赛软件赛省赛第二场 C/C++ 大学B组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。