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]);
    }
}
相关推荐
huohaiyu9 分钟前
Hashtable,HashMap,ConcurrentHashMap之间的区别
java·开发语言·多线程·哈希
信奥卷王1 小时前
[GESP202503 五级] 原根判断
java·数据结构·算法
小咕聊编程1 小时前
【含文档+源码】基于SpringBoot的过滤协同算法之网上服装商城设计与实现
java·spring boot·后端
Zz_waiting.1 小时前
Spring 原理
java·spring·spring自动管理
瓯雅爱分享5 小时前
Java+Vue构建的采购招投标一体化管理系统,集成招标计划、投标审核、在线竞价、中标公示及合同跟踪功能,附完整源码,助力企业实现采购全流程自动化与规范化
java·mysql·vue·软件工程·源代码管理
mit6.8248 小时前
[C# starter-kit] 命令/查询职责分离CQRS | MediatR |
java·数据库·c#
诸神缄默不语8 小时前
Maven用户设置文件(settings.xml)配置指南
xml·java·maven
任子菲阳8 小时前
学Java第三十四天-----抽象类和抽象方法
java·开发语言
学Linux的语莫9 小时前
机器学习数据处理
java·算法·机器学习
找不到、了9 小时前
JVM的即时编译JIT的介绍
java·jvm