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]

代码

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]);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值