[关闭]
@w616561153 2020-06-28T09:27:09.000000Z 字数 3133 阅读 363

csp201912 题解

ccf题解 题解


1

  1. //1805036 <13100928923> <王恪楠> 报数 03-09 13:46 660B C0X 正确 100 15ms 524.0KB
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. queue<int> q;
  5. int vis[50];
  6. bool is_seven(int x) //判断是否是需要跳过的数.
  7. {
  8. if(x % 7 == 0)
  9. return 1;
  10. while(x){
  11. if(x % 10 == 7)
  12. return 1;
  13. x /= 10;
  14. }
  15. return 0;
  16. }
  17. int main()
  18. {
  19. int n;
  20. scanf("%d", &n);
  21. q.push(1); //用类似约瑟夫环的做法,循环报数.
  22. q.push(2);
  23. q.push(3);
  24. q.push(4);
  25. int cnt = 0, i = 0;
  26. while(cnt != n){
  27. i ++;
  28. int head = q.front();
  29. q.pop();
  30. q.push(head);
  31. if(is_seven(i)){
  32. vis[head] ++;
  33. continue;
  34. }
  35. cnt ++;
  36. }
  37. for(int i = 1; i <= 4; i ++)
  38. printf("%d\n", vis[i]);
  39. return 0;
  40. }

2

  1. //1805061 <13100928923> <王恪楠> 回收站选址 03-09 14:06 1.352KB C0X 正确 100 15ms 848.0KB
  2. #include <bits/stdc++.h>
  3. #define fi first
  4. #define se second
  5. using namespace std;
  6. typedef pair<int, int> Pii;
  7. vector<Pii> v;
  8. map<Pii, int> vis; //主要是用map记录下每个点是否有垃圾站.
  9. int cnt[50];
  10. int next_1[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
  11. int next_2[4][2] = {1, 1, 1, -1, -1, 1, -1, -1};
  12. inline int read()
  13. {
  14. int res = 0, f = 1;
  15. char ch;
  16. ch = getchar();
  17. while(48 > ch || ch > 57){
  18. if(ch == '-')
  19. f = -1;
  20. ch = getchar();
  21. }
  22. while(48 <= ch && ch <= 57){
  23. res = (res << 3) + (res << 1) + ch - 48;
  24. ch = getchar();
  25. }
  26. return f * res;
  27. }
  28. int main()
  29. {
  30. int n;
  31. n = read();
  32. for(int i = 1; i <= n; i ++){
  33. int tx, ty;
  34. tx = read(), ty = read();
  35. v.push_back(Pii(tx, ty));
  36. vis[Pii(tx, ty)] = 1;
  37. }
  38. for(int i = 0; i < v.size(); i ++){
  39. int x = v[i].fi, y = v[i].se;
  40. int flag = 0;
  41. for(int j = 0; j < 4; j ++){
  42. int tx = x + next_1[j][0], ty = y + next_1[j][1];
  43. if(!vis[Pii(tx, ty)])
  44. flag = 1;
  45. }
  46. if(flag)
  47. continue;
  48. int score = 0;
  49. for(int j = 0; j < 4; j ++){
  50. int tx = x + next_2[j][0], ty = y + next_2[j][1];
  51. if(vis[Pii(tx, ty)])
  52. score ++;
  53. }
  54. cnt[score] ++;
  55. }
  56. for(int i = 0; i < 5; i ++)
  57. printf("%d\n", cnt[i]);
  58. return 0;
  59. }

3

  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. using namespace std;
  5. typedef pair<int, int> Pii;
  6. map<string, int> vis;
  7. vector<string> split(const string &s, const char &oprt)
  8. {
  9. vector<string> res;
  10. string tmp;
  11. for(int i = 0; i < s.length(); i ++){
  12. if(s[i] != oprt)
  13. tmp += s[i];
  14. else{
  15. if(!tmp.empty())
  16. res.push_back(tmp);
  17. tmp.clear();
  18. }
  19. }
  20. if(!tmp.empty())
  21. res.push_back(tmp);
  22. return res;
  23. }
  24. Pii num(int start, const string &s)
  25. {
  26. int res = 0;
  27. for(int i = start; i < s.length(); i ++){
  28. if(isdigit(s[i]))
  29. res = res * 10 + s[i] - 48;
  30. else
  31. return Pii(res, i - 1);
  32. }
  33. }
  34. void chemical_formula(string s, int flag)
  35. {
  36. stack<string> s1, s2;
  37. map<string, int> vis0[50];
  38. int id = 0;
  39. for(int i = 0; i < s.length(); i ++){
  40. if(s[i] != ')'){
  41. string tmp = " ";
  42. tmp[0] = s[i];
  43. if(s[i] == '('){
  44. s1.push(tmp);
  45. id ++;
  46. }
  47. else{
  48. if('a' <= s[i + 1] && s[i + 1] <= 'z'){
  49. tmp += s[i + 1];
  50. i = i + 1;
  51. }
  52. if(isdigit(s[i + 1])){
  53. Pii p = num(i + 1, s);
  54. vis0[id][tmp] += flag * p.fi;
  55. i = p.se;
  56. }
  57. else
  58. vis0[id][tmp] += flag;
  59. s1.push(tmp);
  60. }
  61. }
  62. else{
  63. int tmp = 1;
  64. if(i + 1 < s.length() && isdigit(s[i + 1])){
  65. Pii p = num(i + 1, s);
  66. tmp = p.fi;
  67. i = p.se;
  68. }
  69. for(map<string, int> :: iterator it = vis0[id].begin(); it != vis0[id].end(); it ++){
  70. vis0[id - 1][it -> fi] += it -> se * tmp;
  71. }
  72. vis0[id].clear();
  73. id --;
  74. }
  75. }
  76. for(map<string, int> :: iterator it = vis0[id].begin(); it != vis0[id].end(); it ++)
  77. vis[it -> fi] += it -> se;
  78. return ;
  79. }
  80. void _find(string s0, int flag)
  81. {
  82. vector<string> v = split(s0, '+');
  83. for(int i = 0; i < v.size(); i ++){
  84. string s = v[i];
  85. if(!isdigit(s[0]))
  86. chemical_formula("(" + s + ")", flag);
  87. else{
  88. int cnt = 0;
  89. for(int j = 0; j < s.length(); j ++){
  90. if(isdigit(s[j])){
  91. cnt = cnt * 10 + s[j] - 48;
  92. }
  93. else{
  94. s = s.substr(j);
  95. break;
  96. }
  97. }
  98. chemical_formula("(" + s + ")", flag * cnt);
  99. }
  100. }
  101. return;
  102. }
  103. int main()
  104. {
  105. int n;
  106. scanf("%d", &n);
  107. while(n --){
  108. string s;
  109. cin >> s;
  110. vis.clear();
  111. vector<string> v = split(s, '=');
  112. _find(v[0], 1);
  113. _find(v[1], -1);
  114. int flag = 0;
  115. for(map<string, int> :: iterator it = vis.begin(); it != vis.end(); it ++){
  116. if(it -> second)
  117. flag = 1;
  118. }
  119. if(!flag)
  120. printf("Y\n");
  121. else
  122. printf("N\n");
  123. }
  124. return 0;
  125. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注