笔试强训Day8 基础数学 动态规划

求最小公倍数

题目链接:求最小公倍数__牛客网 (nowcoder.com)

思路:

求两个数的最小公倍数 即 两个数的乘积 / 两个数的最大公约数 直接辗转相除法即可

记得开long long。

AC code:

#include <iostream>
using namespace std;
typedef long long LL;
int a, b;
LL mul;
int func(int a, int b)
{
    return b ? func(b, a % b) : a;
}
int main()
{
    cin >> a >> b;
    mul = (LL)a * b;
    int c = func(a,b);
    
    cout << mul / c;
}

NC95 数组中的最长连续子序列

题目链接:数组中的最长连续子序列_牛客题霸_牛客网 (nowcoder.com)

思路:基础动态规划 但是题目简单 直接排序去重 暴力一遍即可。

AC code:

class Solution {
public:

    int MLS(vector<int>& arr) 
    {
        int ans = 1;
        int tmp = 1;
        sort(arr.begin(), arr.end());
        arr.erase(unique(arr.begin(), arr.end()), arr.end());

        for(int i = 1; i < arr.size(); i ++)
        {
            if(arr[i] == arr[i - 1] + 1)
            {
                tmp ++;
                ans = max(ans,tmp);
            }
            else 
            {
                tmp = 1;
            }
            
        }
        return ans;
    }
};

DP39 字母收集

题目链接:字母收集_牛客题霸_牛客网 (nowcoder.com)

思路:基础dp 每一个点位的状态 由它上面的格子和左边的格子确定 两者求最大值即可

AC code:

#include<iostream>
#include<map>
#include<vector>
const int N = 510;
using namespace std;
int dp[N][N];
map<char,int> check{{'l',4},{'o',3},{'v',2},{'e',1}};
int main()
{
    int n, m;
    cin >> n >> m;
    char op;
    getchar();
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            scanf("%c",&op);
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + check[op];
        }
        getchar();
    }
    cout << dp[n][m] << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值