当前位置:   article > 正文

C++笔试强训day19

C++笔试强训day19

目录

1.小易的升级之路

2.礼物的最大价值

3.对称之美


1.小易的升级之路

链接

模拟就行,唯一可能是难点得就是gcd(最大公约数)

  1. #include <iostream>
  2. using namespace std;
  3. #define int long long
  4. const int N = 1e5 + 10;
  5. int arr[N];
  6. int gcd(int a, int b)
  7. {
  8. return b == 0 ? a : gcd(b, a % b);
  9. }
  10. signed main() {
  11. int n, init;
  12. cin >> n >> init;
  13. for (int i = 0; i < n; ++i)
  14. cin >> arr[i];
  15. for (int i = 0; i < n; ++i)
  16. if (init >= arr[i])
  17. init += arr[i];
  18. else
  19. init += gcd(init, arr[i]);
  20. cout << init << endl;
  21. return 0;
  22. }

2.礼物的最大价值

链接

动态规划  -  路径问题。

  1. typedef long long LL;
  2. class Solution {
  3. public:
  4. const static int N = 300;
  5. LL dp[N][N] = { 0 };
  6. int maxValue(vector<vector<int>>& grid) {
  7. int n = grid.size();
  8. int m = grid[0].size();
  9. for (int i = 1; i <= n; ++i)
  10. for (int j = 1; j <= m; ++j)
  11. dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
  12. return dp[n][m];
  13. }
  14. };

3.对称之美

链接


从第1个字符串一直到第n个字符串每个串取一个字母来构成一个新字符串,新字符串的第i个字母只能从第i行的字符串中选出。

根据题意:

我们可以从两边开始(一个下标为0, 一个下标为 n - 1)开始遍历,如果一路遍历下来,所有的两边两个字符串中都有相同的字符,则返回Yes,若有一个没有则No

详细代码:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. vector<string> vs(110);
  5. // 哈希判断是否有相同字符
  6. bool Check(string s, string t)
  7. {
  8. int hash[128] = { 0 };
  9. for (auto c : s)
  10. hash[c]++;
  11. for (auto c : t)
  12. if (hash[c] != 0)
  13. return true;
  14. return false;
  15. }
  16. // 双指针
  17. void solve()
  18. {
  19. int n;
  20. cin >> n;
  21. for (int i = 0; i < n; ++i)
  22. cin >> vs[i];
  23. int flag = 1;
  24. int l = 0, r = n - 1;
  25. while (l < r)
  26. {
  27. if (!Check(vs[l], vs[r]))
  28. flag = 0;
  29. l++;
  30. r--;
  31. }
  32. if (flag == 1)
  33. cout << "Yes" << endl;
  34. else
  35. cout << "No" << endl;
  36. }
  37. int main() {
  38. int t;
  39. cin >> t;
  40. while (t--)
  41. solve();
  42. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/563516
推荐阅读
相关标签
  

闽ICP备14008679号