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]);
    }
}
相关推荐
橙子圆12322 分钟前
JUC之集合类不安全
java·开发语言
谢亮_vipxieliang1 小时前
Stream API 不是银弹:常见坑与性能建议
java·开发语言
源码宝2 小时前
Web‑PACS 云影像源码|Java+Vue3 医院影像管理系统完整方案
java·源码·影像pacs·成品源码·医院影像系统
ShineWinsu2 小时前
对于Redis:string类型的解析
java·c++·redis·分布式·缓存·面试·string
三8444 小时前
Fastjson 漏洞 · 02 · autoType 机制与 checkAutoType
java·fastjson
无限码力4 小时前
小红书笔试真题 9.17 - 待发热度重排(C++/Py/Java /Js/Go)
java·小红书·小红书笔试真题·小红书技术岗笔试真题
卓怡学长4 小时前
w194基于ssm新能源汽车充电系统小程序
java·小程序·intellij-idea
JAVA面经实录9174 小时前
Java高级后端 · 全套面试通关手册(MySQL)
java·mysql·面试
光依旧5 小时前
MCP实战手记(八):从“能跑“到“能上线“——无状态MCP Server的生产落地清单
java·人工智能·spring boot·架构·ai agent·mcp