笔试强训(八)

一. 求最小公倍数

题目链接:

https://nowcoder.com/practice/22948c2cad484e0291350abad86136c3?tpId=37&tqId=21331&ru=/exam/oj

cpp 复制代码
#include <iostream>
using namespace std;

int gcd(int a, int b)
{
    if(b == 0) return a;
    return gcd(b, a % b);
}

int main()
{
    int a, b;
    cin >> a >> b;
    cout << (a * b / gcd(a, b)) << endl;
    return 0;
}

二.数组中的最长连续子序列

题目链接:

https://www.nowcoder.com/practice/eac1c953170243338f941959146ac4bf?tpId=196&tqId=37143&ru=/exam/oj

cpp 复制代码
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * max increasing subsequence
     * @param arr int整型vector the array
     * @return int整型
     */
    int MLS(vector<int>& arr) {
        // write code here
        sort(arr.begin(), arr.end());
        int n = arr.size(), ret = 0;
        for (int i = 0; i < n;)
        {
            int j = i + 1, count = 1;
            while (j < n)
            {
                if (arr[j] - arr[j - 1] == 1)
                {
                    count++;
                    j++;
                }
                else if (arr[j] - arr[j - 1] == 0)
                {
                    j++;
                }
                else
                {
                    break;
                }
            }
            ret = max(ret, count);
            i = j;
        }

        return ret;
    }
};

三.字母收集

题目链接:

https://www.nowcoder.com/practice/9740ce2df0a04399a5ade1927d34c1e1?tpId=230&tqId=38954&ru=/exam/oj

cpp 复制代码
#include<iostream>

using namespace std;
int dp[505][505];
int main()
{
	int n, m;
	cin >> n >> m;
	char a[n][m];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> a[i][j];
		}
	}
	
	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]);
			if (a[i-1][j-1] == 'l')
			{
				dp[i][j] += 4;
			}
			else if (a[i-1][j-1] == 'o')
			{	
				dp[i][j] += 3;
			}
			else if (a[i-1][j-1] == 'v')
			{
				dp[i][j] += 2;
			}
			else if (a[i-1][j-1] == 'e')
			{
				dp[i][j] += 1;
			}
		}
	}
	cout << dp[n][m] << endl;
	return 0;
}
相关推荐
步菲5 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
aigcapi9 小时前
RAG 系统的黑盒测试:从算法对齐视角解析 GEO 优化的技术指标体系
大数据·人工智能·算法
知远同学10 小时前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
小徐Chao努力10 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
CoderCodingNo10 小时前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
柯慕灵10 小时前
7大推荐系统/算法框架对比
算法·推荐算法
adam-liu11 小时前
Fun Audio Chat 论文+项目调研
算法·语音端到端·fun-audio-chat
kylezhao201911 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
啃火龙果的兔子11 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
栀秋66611 小时前
你会先找行还是直接拍平?两种二分策略你Pick哪个?
前端·javascript·算法