您的位置:首页 > 健康 > 美食 > 电子工程师是干啥的_新闻20条摘抄大全_站长工具查询官网_东莞seo建站排名

电子工程师是干啥的_新闻20条摘抄大全_站长工具查询官网_东莞seo建站排名

2025/7/6 16:57:58 来源:https://blog.csdn.net/W2155/article/details/143100332  浏览:    关键词:电子工程师是干啥的_新闻20条摘抄大全_站长工具查询官网_东莞seo建站排名
电子工程师是干啥的_新闻20条摘抄大全_站长工具查询官网_东莞seo建站排名

目录

1.添加逗号

 2.跳台阶

3.扑克牌顺子


1.添加逗号

添加逗号_牛客题霸_牛客网

 算法思路:

按照提议模拟即可,从后向前遍历字符串,遍历三个字符之后,将其插入将这三个字符插入到新的字符串中再加上逗号。

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;int main() 
{string s;cin >> s;int n = s.size();string ret;int cnt = 1;for(int i = n - 1; i >= 0; i--){ret += s[i];if(i != 0 && cnt == 3){cnt = 0;ret += ',';}cnt++;}reverse(ret.begin(), ret.end());cout << ret << endl;return 0;
}
// 64 位输出请用 printf("%lld")

 2.跳台阶

跳台阶_牛客题霸_牛客网

算法思路:

动态规划的入门题

1.状态分析: dp[i] i级台阶的跳法

2.状态转移方程:以最后一步的状态推出状态转移方程,青蛙只能跳一步或者两步,所以到达i位置无非就是两种情况从i-1跳或者从i-2跳 dp[i] = dp[i -1] + dp[i - 2]。

#include <iostream>
using namespace std;// 1 2 3 4
// a b c
//   a b c
int main() 
{int n = 0; cin >> n;int a = 1, b = 1,c = 0;for(int i = 2; i <= n; i++){c = a + b;a = b;b = c;}if(n ==0 || n == 1)cout << n << endl;elsecout << c << endl;return 0;
}
// 64 位输出请用 printf("%lld")

3.扑克牌顺子

扑克牌顺子_牛客题霸_牛客网

算法思路:

这道题可以使用逆向思维,哪5张扑克牌不能组成顺子。

1.出现重复的牌,一定组不成顺子。

2.5张扑克,最大的牌 - 最小的牌 差值大于 4 也一定组不成顺子。

#include <functional>
class Solution {
public:
//2 0 0 4 6bool IsContinuous(vector<int>& numbers) {int Max = 0;int Min = 14;int n = numbers.size();bool hash[14] = {0};for(auto a : numbers){if(a != 0){if(hash[a]){return false;}hash[a] = true; Max = max(a, Max);Min = min(a,Min);       }}return Max - Min <= 4;}
};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com