笔试强训(八)

一. 求最小公倍数

题目链接:

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;
}
相关推荐
小陈工1 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
海清河晏1111 小时前
数据结构 | 单循环链表
数据结构·算法·链表
H Journey1 小时前
C++之 CMake、CMakeLists.txt、Makefile
开发语言·c++·makefile·cmake
wuweijianlove5 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
_dindong5 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志5 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
lly2024066 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨6 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn9996 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏4946 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源