飞道的博客

2020年蓝桥杯省赛第二场C++B组题解

376人阅读  评论(0)

A.

暴力计算即可。


  
  1. // #pragma GCC optimize(2)
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <stack>
  10. #include <map>
  11. #include <sstream>
  12. #include <cstring>
  13. #include <set>
  14. #include <cctype>
  15. #include <bitset>
  16. #define IO \
  17. ios::sync_with_stdio(false); \
  18. // cout.tie(0);
  19. using namespace std;
  20. // int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
  21. typedef unsigned long long ULL;
  22. typedef long long LL;
  23. typedef pair< int, int> P;
  24. const int maxn = 2e8 + 10;
  25. const int maxm = 2e5 + 10;
  26. const LL INF = 0x3f3f3f3f3f3f3f3f;
  27. const int inf = 0x3f3f3f3f;
  28. const LL mod = 1e9 + 7;
  29. const double eps = 1e-8;
  30. const double pi = acos( -1);
  31. int dis[ 4][ 2] = { 1, 0, 0, -1, 0, 1, -1, 0};
  32. int m[ 13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. int ans = 0;
  34. int count(int n)
  35. {
  36. int cnt = 0;
  37. while (n)
  38. {
  39. int t = n % 10;
  40. if (t == 2)
  41. cnt++;
  42. n = n / 10;
  43. }
  44. return cnt;
  45. }
  46. int main()
  47. {
  48. #ifdef WXY
  49. freopen( "in.txt", "r", stdin);
  50. // freopen("out.txt", "w", stdout);
  51. #endif
  52. IO;
  53. for ( int i = 1; i <= 2020; i++)
  54. ans += count(i);
  55. cout << ans;
  56. return 0;
  57. }

624

容易找规律发现相邻两项之间的关系。


  
  1. // #pragma GCC optimize(2)
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <stack>
  10. #include <map>
  11. #include <sstream>
  12. #include <cstring>
  13. #include <set>
  14. #include <cctype>
  15. #include <bitset>
  16. #define IO \
  17. ios::sync_with_stdio(false); \
  18. // cout.tie(0);
  19. using namespace std;
  20. // int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
  21. typedef unsigned long long ULL;
  22. typedef long long LL;
  23. typedef pair< int, int> P;
  24. const int maxn = 2e8 + 10;
  25. const int maxm = 2e5 + 10;
  26. const LL INF = 0x3f3f3f3f3f3f3f3f;
  27. const int inf = 0x3f3f3f3f;
  28. const LL mod = 1e9 + 7;
  29. const double eps = 1e-8;
  30. const double pi = acos( -1);
  31. int dis[ 4][ 2] = { 1, 0, 0, -1, 0, 1, -1, 0};
  32. int m[ 13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. int main()
  34. {
  35. #ifdef WXY
  36. freopen( "in.txt", "r", stdin);
  37. // freopen("out.txt", "w", stdout);
  38. #endif
  39. IO;
  40. int a[ 50];
  41. int t = 1;
  42. a[ 1] = 1;
  43. for ( int i = 2; i <= 20;i++)
  44. a[i] = a[i - 1] + t * 4, t++;
  45. cout << a[ 20];
  46. return 0;
  47. }

761

C.

暴力判断即可。


  
  1. // #pragma GCC optimize(2)
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <stack>
  10. #include <map>
  11. #include <sstream>
  12. #include <cstring>
  13. #include <set>
  14. #include <cctype>
  15. #include <bitset>
  16. #define IO \
  17. ios::sync_with_stdio(false); \
  18. // cout.tie(0);
  19. using namespace std;
  20. // int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
  21. typedef unsigned long long ULL;
  22. typedef long long LL;
  23. typedef pair< int, int> P;
  24. const int maxn = 2e8 + 10;
  25. const int maxm = 2e5 + 10;
  26. const LL INF = 0x3f3f3f3f3f3f3f3f;
  27. const int inf = 0x3f3f3f3f;
  28. const LL mod = 1e9 + 7;
  29. const double eps = 1e-8;
  30. const double pi = acos( -1);
  31. int dis[ 4][ 2] = { 1, 0, 0, -1, 0, 1, -1, 0};
  32. int m[ 13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. int gcd(int a, int b)
  34. {
  35. if (b == 0)
  36. return a;
  37. else
  38. return gcd(b, a % b);
  39. }
  40. int main()
  41. {
  42. #ifdef WXY
  43. freopen( "in.txt", "r", stdin);
  44. // freopen("out.txt", "w", stdout);
  45. #endif
  46. IO;
  47. int ans = 0;
  48. for ( int i = 1; i <= 2020; i++)
  49. for ( int j = 1; j <= 2020; j++)
  50. if (gcd(i, j) == 1)
  51. ans++;
  52. cout << ans;
  53. return 0;
  54. }

2481215

 

D.

比赛的时候因为怕数迷,就没用日历表数,现在回想一下真该检查的时候数一遍。

这题我写的8880,就是因为打印函数返回值出的锅(调用函数会导致全局变量改变),但凡当时用变量存一下函数返回值也不会错了,一念之间。。。省一没了。。。。


  
  1. // #pragma GCC optimize(2)
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <stack>
  10. #include <map>
  11. #include <sstream>
  12. #include <cstring>
  13. #include <set>
  14. #include <cctype>
  15. #include <bitset>
  16. #define IO \
  17. ios::sync_with_stdio(false); \
  18. // cout.tie(0);
  19. using namespace std;
  20. // int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
  21. typedef unsigned long long ULL;
  22. typedef long long LL;
  23. typedef pair< int, int> P;
  24. const int maxn = 2e8 + 10;
  25. const int maxm = 2e5 + 10;
  26. const LL INF = 0x3f3f3f3f3f3f3f3f;
  27. const int inf = 0x3f3f3f3f;
  28. const LL mod = 1e9 + 7;
  29. const double eps = 1e-8;
  30. const double pi = acos( -1);
  31. int dis[ 4][ 2] = { 1, 0, 0, -1, 0, 1, -1, 0};
  32. int m[ 13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. int w = 5;
  34. bool rui(int n)
  35. {
  36. if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
  37. return true;
  38. return false;
  39. }
  40. int count(int year)
  41. {
  42. int cnt = 0;
  43. if (rui(year))
  44. m[ 2] += 1;
  45. if (year == 2020)
  46. {
  47. for ( int i = 1; i <= 9; i++)
  48. {
  49. for ( int j = 1; j <= m[i]; j++)
  50. {
  51. if (w == 0 || j == 1)
  52. {
  53. cnt++;
  54. }
  55. w = (w + 1) % 7;
  56. }
  57. }
  58. }
  59. else
  60. {
  61. for ( int i = 1; i <= 12; i++)
  62. {
  63. for ( int j = 1; j <= m[i]; j++)
  64. {
  65. if (w == 0 || j == 1)
  66. {
  67. cnt++;
  68. }
  69. w = (w + 1) % 7;
  70. }
  71. }
  72. }
  73. if (rui(year))
  74. m[ 2] -= 1;
  75. return cnt;
  76. }
  77. int main()
  78. {
  79. #ifdef WXY
  80. freopen( "in.txt", "r", stdin);
  81. // freopen("out.txt", "w", stdout);
  82. #endif
  83. IO;
  84. int ans = 0;
  85. for ( int i = 2000; i <= 2020; i++)
  86. {
  87. // cout << count(i) << endl;
  88. // 一念之间 天堂地狱
  89. // 闲的没事非打印干嘛?!
  90. // 打印完了还不注释掉?!
  91. ans += count(i);
  92. }
  93. cout << ans + 7580 + 1; // 最后再加上10.1这一天
  94. return 0;
  95. }

8879

E.

可以用二维数组建图然后跑bfs判断是否连通。


  
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int dis[6][2] = { 1, -1, -1, 1, -1, -1, 1, 1, 2, 0, -2, 0} ;
  4. struct Node
  5. {
  6. int x, y;
  7. Node(){} ;
  8. Node(int a, int b)
  9. {
  10. x = a, y = b;
  11. }
  12. } ;
  13. int g[10][10];
  14. bool book[10][10];
  15. bool vis[10];
  16. int a[10];
  17. int C, ans;
  18. bool BFS(int all)
  19. {
  20. memset(book, false, sizeof book);
  21. int sx, sy;
  22. for (int i = 1 ; i <= 5 ; i++)
  23. for (int j = 1 ; j <= 3 ; j++)
  24. if (g[ i][ j] == 1 )
  25. {
  26. sx = i, sy = j;
  27. break;
  28. }
  29. queue<Node> q;
  30. q.push(Node(sx, sy));
  31. book[ sx][ sy] = true ;
  32. int cnt = 0 ;
  33. while (!q.empty())
  34. {
  35. Node now = q.front();
  36. q.pop();
  37. cnt++;
  38. if (now.y == 2 )
  39. {
  40. for (int i = 0 ; i < 4 ; i++)
  41. {
  42. int tx = now.x + dis[ i][ 0] ;
  43. int ty = now.y + dis[ i][ 1] ;
  44. if (tx >= 1 && tx <= 5 && ty >= 1 && ty <= 3 && g[ tx][ ty] == 1 && book[ tx][ ty] == false )
  45. {
  46. book[ tx][ ty] = true ;
  47. q.push(Node(tx, ty));
  48. }
  49. }
  50. }
  51. else
  52. {
  53. for (int i = 0 ; i < 6 ; i++)
  54. {
  55. int tx = now.x + dis[ i][ 0] ;
  56. int ty = now.y + dis[ i][ 1] ;
  57. if (tx >= 1 && tx <= 5 && ty >= 1 && ty <= 3 && g[ tx][ ty] == 1 && book[ tx][ ty] == false )
  58. {
  59. book[ tx][ ty] = true ;
  60. q.push(Node(tx, ty));
  61. }
  62. }
  63. }
  64. }
  65. return cnt == all;
  66. }
  67. void draw(int val)
  68. {
  69. if (val == 1 )
  70. g[ 1][ 2] = 1 ;
  71. if (val == 2 )
  72. g[ 2][ 3] = 1 ;
  73. if (val == 3 )
  74. g[ 4][ 3] = 1 ;
  75. if (val == 4 )
  76. g[ 5][ 2] = 1 ;
  77. if (val == 5 )
  78. g[ 4][ 1] = 1 ;
  79. if (val == 6 )
  80. g[ 2][ 1] = 1 ;
  81. if (val == 7 )
  82. g[ 3][ 2] = 1 ;
  83. return;
  84. }
  85. void DFS(int now, int sum)
  86. {
  87. if (sum == C)
  88. {
  89. memset(g, 0, sizeof g);
  90. for (int i = 0 ; i < sum; i++)
  91. draw(a[ i] );
  92. if (BFS(sum))
  93. ans++;
  94. return;
  95. }
  96. for (int i = now; i <= 7 ; i++)
  97. {
  98. if (vis[ i] == false )
  99. {
  100. vis[ i] = true ;
  101. a[ sum] = i;
  102. DFS(i + 1, sum + 1 );
  103. vis[ i] = false ;
  104. }
  105. }
  106. return;
  107. }
  108. int main()
  109. {
  110. for (int i = 1 ; i <= 7 ; i++)
  111. {
  112. C = i;
  113. DFS(1, 0 );
  114. }
  115. cout << ans;
  116. return 0 ;
  117. }

80

F


  
  1. // #pragma GCC optimize(2)
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <stack>
  10. #include <map>
  11. #include <sstream>
  12. #include <cstring>
  13. #include <set>
  14. #include <cctype>
  15. #include <bitset>
  16. #define IO \
  17. ios::sync_with_stdio(false); \
  18. // cout.tie(0);
  19. using namespace std;
  20. // int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
  21. typedef unsigned long long ULL;
  22. typedef long long LL;
  23. typedef pair< int, int> P;
  24. const int maxn = 2e8 + 10;
  25. const int maxm = 2e5 + 10;
  26. const LL INF = 0x3f3f3f3f3f3f3f3f;
  27. const int inf = 0x3f3f3f3f;
  28. const LL mod = 1e9 + 7;
  29. const double eps = 1e-8;
  30. const double pi = acos( -1);
  31. int dis[ 6][ 2] = { 1, -1, -1, 1, -1, -1, 1, 1, 2, 0, -2, 0};
  32. int m[ 13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. int main()
  34. {
  35. #ifdef WXY
  36. freopen( "in.txt", "r", stdin);
  37. // freopen("out.txt", "w", stdout);
  38. #endif
  39. IO;
  40. int n, x;
  41. int cnt1, cnt2 = 0;
  42. char c = '%';
  43. cin >> n;
  44. for ( int i = 0; i < n; i++)
  45. {
  46. cin >> x;
  47. if (x >= 60)
  48. cnt1++;
  49. if (x >= 85)
  50. cnt2++;
  51. }
  52. printf( "%.0lf", round( 100.0 * double(cnt1) / double(n)));
  53. printf( "%c\n", c);
  54. printf( "%.0lf", round( 100.0 * double(cnt2) / double(n)));
  55. printf( "%c", c);
  56. return 0;
  57. }

G

当时抽疯写了个200+的弱智暴力(还不一定能对),其实只要枚举年份构造出回文串,然后把符合条件的日期存起来,二分答案即可。

H

太菜了只会暴力做,O(n^3)

是牛客多校上一道原题。

统计每个字符,对每个字符记录它上一次出现的位置vis,当前字符所能贡献的区间个数为(i-pre)*(n-i+1),i为当前字符下标。


  
  1. // #pragma GCC optimize(2)
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <random>
  8. #include <cctype>
  9. #include <bitset>
  10. #include <string>
  11. #include <vector>
  12. #include <queue>
  13. #include <cmath>
  14. #include <stack>
  15. #include <set>
  16. #include <map>
  17. #define IO \
  18. ios::sync_with_stdio(false); \
  19. // cout.tie(0);
  20. using namespace std;
  21. // int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
  22. typedef unsigned long long ULL;
  23. typedef long long LL;
  24. typedef pair< int, int> P;
  25. const int maxn = 1e5 + 10;
  26. const int maxm = 1e6 + 10;
  27. const LL INF = 0x3f3f3f3f3f3f3f3f;
  28. const int inf = 0x3f3f3f3f;
  29. const LL mod = 1e9 + 7;
  30. const double eps = 1e-8;
  31. const double pi = acos( -1);
  32. int dis[ 4][ 2] = { 1, 0, 0, -1, 0, 1, -1, 0};
  33. // int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  34. LL vis[maxn];
  35. char s[maxn];
  36. int main()
  37. {
  38. #ifdef WXY
  39. freopen( "in.txt", "r", stdin);
  40. // freopen("out.txt", "w", stdout);
  41. #endif
  42. scanf( "%s", s + 1);
  43. int n = strlen(s + 1);
  44. LL ans = 0;
  45. for ( int i = 1; i <= n; i++)
  46. {
  47. int t = s[i] - 'a' + 1;
  48. ans = ans + LL(i - vis[t]) * LL(n - i + 1);
  49. vis[t] = i;
  50. }
  51. cout << ans;
  52. return 0;
  53. }

I

输出样例

当时如果仔细想想,应该能把n<=4的情况暴力模拟出来。。

J

输出样例

不出意外,省一应该没了,还是太菜了。。。。

2020.10.27更新

居然省一了,本来以为填空全对才能摸到省一,没想到蓝桥杯这么水。。。

 


转载:https://blog.csdn.net/qq_44115065/article/details/109141107
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场