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

前言

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

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

题目描述

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

思路

1.动态规划:scorei=max(scorei-1~scorei-k)+scorei

2.打印scorei-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]);
    }
}
相关推荐
NE_STOP9 小时前
Vide Coding--AI编程工具的选择
java
码云数智-园园9 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆9 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz10 小时前
Maven依赖冲突
java·服务器·maven
swordbob10 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯10 小时前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
十五喵源码网10 小时前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
摇滚侠10 小时前
IDEA 创建 Java 项目 手动整合 SSM 框架
java·ide·intellij-idea
源分享10 小时前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Flittly10 小时前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring