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]);
    }
}
相关推荐
满满和米兜3 分钟前
【Java基础】- 集合-HashSet与TreeSet
java·开发语言·算法
网安INF15 分钟前
数据结构第二章复习:线性表
java·开发语言·数据结构
aq553560018 分钟前
Laravel10.X核心特性全解析
java·开发语言·spring boot·后端
锦瑟弦音34 分钟前
Java与SQL基础知识总结
java·开发语言
停水咋洗澡37 分钟前
Redis Sentinel高可用实战:主从自动故障转移
java·redis·sentinel
ch.ju40 分钟前
Java程序设计(第3版)第二章——引用数据类型:String
java
W230357657341 分钟前
经典算法详解:最大子数组和(暴力 / 分治 / 动态规划 / 线段树)
算法·动态规划·最大字段和
yaoxin5211231 小时前
376. Java IO API - 使用 Globbing 和自定义 Filter 过滤目录内容
java·开发语言·python
手握风云-1 小时前
JavaEE 初阶第三十一期:JVM,一次Full GC的架构级思考(下)
java·java-ee