笔试强训Day9 字符串 动态规划 模拟

BC146 添加逗号

题目链接:添加逗号_牛客题霸_牛客网 (nowcoder.com)

思路:倒着读一遍 每隔三个字符加一个逗号 即可

AC code:

#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
string a,b;
int main() 
{
    cin >> a;
    int cnt = 0;
    for(int i = a.size() - 1; i >= 0; i --)
    {
        
        if(cnt % 3 == 0 && cnt)
        {
            b += ',', b += a[i];
        }
        else b += a[i];

        cnt ++;
    }

    reverse(b.begin(), b.end());

    cout << b << endl;
}

DP2 跳台阶

题目链接:跳台阶_牛客题霸_牛客网 (nowcoder.com)

思路:类斐波那契数列 每一项的状态由前两项 决定 , 取和。数据量小

直接预处理一遍输出结果即可。

AC code:

#include <iostream>
using namespace std;
typedef long long LL;
const int N = 50;
int n;
LL dp[N] = {1,2};
int main()
{
    for(int i = 2; i < 45; i ++)
        dp[i] = dp[i - 1] + dp[i - 2];
    
    cin >> n;
    cout << dp[n - 1];
}

JZ61 扑克牌顺子

题目链接:扑克牌顺子_牛客题霸_牛客网 (nowcoder.com)

思路:模拟题 五张牌 统计0的个数 记录最大值最小值 用 bool 数组 查询是否能凑齐

[left, right] 区间内的所有牌。

注意特判

① 有四张 0 直接 return 1 即可;

② 有两张相同的牌 直接 return 0 即可

AC code:

class Solution {
public:

    bool IsContinuous(vector<int>& numbers) 
    {
        bool st[20];
        for(int i = 0; i <= 19; i ++) st[i] = 0;
        int cnt = 0;
        int right = -99;
        int left = 99;
        for(int i = 0; i < numbers.size(); i ++)
        {
            if(!numbers[i])
            {
                cnt ++; continue;
            }
            
            if(st[numbers[i]]) return 0;

            st[numbers[i]] = 1;
            left = min(left, numbers[i]);
            right = max(right, numbers[i]);
        }
        if(cnt == 4) return true;

        for(int i = left; i <= right; i ++)
        {
            if(!st[i] && cnt)
            {
                st[i] = 1; cnt --;
            }
            
            if(!st[i])
                return 0;
        }
        return 1;
    }
};

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值