2023华为od机试C卷【跳格子3】

前言

博主刷的华为机考题,代码仅供参考,因为没有后台数据,可能有没考虑到的情况

如果感觉对你有帮助,请点点关注点点赞吧,谢谢你!

题目描述

复制代码
输入
6
1 -1 -6 7 -17 7
2
输出
14

思路

1.动态规划:score[i]=max(score[i-1]~score[i-k])+score[i]

2.打印score[i-1]

代码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        int[] score = new int[n];
        for (int i = 0; i < n; i++) {
            score[i] = sc.nextInt();
        }
        sc.nextLine();
        int k=sc.nextInt();
        sc.close();
        for (int i = 1; i < n; i++) {
            int max=Integer.MIN_VALUE;
            for (int j = i-1; j>=i-k&&j>=0; j--) {
                max=Math.max(max,score[j]);
            }
            score[i]+=max;
        }
        System.out.println(score[n-1]);
    }
}
相关推荐
2601_9577867710 分钟前
多平台矩阵运营的底层逻辑:当账号管理、内容生产与线索转化被一条链路串起来
java·数据库·矩阵·多平台管理
代码中介商13 分钟前
排序算法完全指南(六):希尔排序深度详解
java·算法·排序算法
布吉岛的石头29 分钟前
Java 程序员第 22 阶段:Function Call 工具调用实战,Java 封装大模型外部能力
java·人工智能·python
阿维的博客日记37 分钟前
线程任务执行报错后,线程会不会挂掉,Java线程池
java·线程池
Hwang25238 分钟前
Spring 框架- 容器单例池的理解
java
yh弓长39 分钟前
算法积累笔记
java·算法
LeocenaY41 分钟前
C/C++ 面试题总结
java·c++·面试
雨落在了我的手上1 小时前
初识java(十一):继承
java·开发语言
XS0301061 小时前
MyBatis关联映射
java·mybatis
码农小旋风1 小时前
IDEA 不只接 Claude 和 Codex:本地模型和第三方 API 也能直接用
java·ide·人工智能·chatgpt·intellij-idea·claude