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]);
    }
}
相关推荐
飛_1 小时前
解决VSCode无法加载Json架构问题
java·服务器·前端
木棉软糖4 小时前
一个MySQL的数据表最多能够存多少的数据?
java
程序视点4 小时前
Java BigDecimal详解:小数精确计算、使用方法与常见问题解决方案
java·后端
愿你天黑有灯下雨有伞4 小时前
Spring Boot SSE实战:SseEmitter实现多客户端事件广播与心跳保活
java·spring boot·spring
Java初学者小白5 小时前
秋招Day20 - 微服务
java
狐小粟同学5 小时前
JavaEE--3.多线程
java·开发语言·java-ee
程序员-King.6 小时前
day69—动态规划—爬楼梯(LeetCode-70)
算法·动态规划
KNeeg_6 小时前
Spring循环依赖以及三个级别缓存
java·spring·缓存
AI_Gump7 小时前
【AI阅读】20250717阅读输入
java·spring boot·spring
找不到、了8 小时前
Java排序算法之<插入排序>
java·算法·排序算法