当前位置: 首页 > news >正文

C++笔试强训day19

目录

1.小易的升级之路

2.礼物的最大价值

3.对称之美


1.小易的升级之路

链接

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

#include <iostream>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int arr[N];
int gcd(int a, int b)
{return b == 0 ? a : gcd(b, a % b);
}
signed main() {int n, init;cin >> n >> init;for (int i = 0; i < n; ++i)cin >> arr[i];for (int i = 0; i < n; ++i)if (init >= arr[i])init += arr[i];elseinit += gcd(init, arr[i]);cout << init << endl;return 0;
}

2.礼物的最大价值

链接

动态规划  -  路径问题。

typedef long long LL;
class Solution {
public:const static int N = 300;LL dp[N][N] = { 0 };int maxValue(vector<vector<int>>& grid) {int n = grid.size();int m = grid[0].size();for (int i = 1; i <= n; ++i)for (int j = 1; j <= m; ++j)dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];return dp[n][m];}
};

3.对称之美

链接


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

根据题意:

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

详细代码:

#include <iostream>
#include <vector>
using namespace std;
vector<string> vs(110);
// 哈希判断是否有相同字符
bool Check(string s, string t)
{int hash[128] = { 0 };for (auto c : s)hash[c]++;for (auto c : t)if (hash[c] != 0)return true;return false;
}
// 双指针
void solve()
{int n;cin >> n;for (int i = 0; i < n; ++i)cin >> vs[i];int flag = 1;int l = 0, r = n - 1;while (l < r){if (!Check(vs[l], vs[r]))flag = 0;l++;r--;}if (flag == 1)cout << "Yes" << endl;elsecout << "No" << endl;
}
int main() {int t;cin >> t;while (t--)solve();
}
http://www.lryc.cn/news/347659.html

相关文章:

  • MySQL软件安装基于压缩包
  • 04 贝尔曼最优公式
  • 印象笔记使用技巧
  • 产品设计中的“注册”说明
  • 【linux学习】多线程(1)
  • Leetcode 3149. Find the Minimum Cost Array Permutation
  • Python | 为列表中的元素分配唯一值
  • HTML炫酷的相册
  • C++笔试强训day20
  • 【PHP【实战项目】系统性教学】——使用最精简的代码完成用户的登录与退出
  • Linux下的常用基本指令
  • phpstorm环境配置与应用
  • 【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout
  • Hive Aggregation 聚合函数
  • Unity 性能优化之GPU Instancing(五)
  • LeetCode 138. 随机链表的复制
  • 【PC微信小程序点不动处理方法】
  • 量化交易:日内网格交易策略.md
  • Ubuntu 20.04在Anaconda虚拟环境中配置PyQt4
  • charts3D地球--添加航线
  • 变色龙还是树懒:揭示大型语言模型在知识冲突中的行为
  • Android OpenMAX(四)OMX Core
  • 【Linux】轻量级应用服务器如何开放端口 -- 详解
  • git如何查看密码
  • redis脑裂问题
  • 日本率先研发成功6G设备,刺痛了谁?为何日本能率先突破?
  • SpringBoot自动配置源码解析+自定义Spring Boot Starter
  • Kafka 环境配置与使用总结
  • 【算法】滑动窗口——串联所有单词的子串
  • 等保测评安全物理环境测评讲解