华为OD机试——最多获得的短信条数、云短信平台优惠活动——java
完全背包
题目描述
某云短信厂商,为庆祝国庆,推出充值优惠活动。
现在给出客户预算,和优惠售价序列,求最多可获得的短信总条数。
输入描述
第一行客户预算M,其中 0 <=M <= 10^6
第二行给出售价表,P1,P2,... Pn,其中 1<= n <= 100
Pi 为充值 i 元获得的短信条数。1<=Pi<= 1000,1 <=n<=100
输出描述
最多获得的短信条数
public class test23 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = scanner.nextInt(); Integer[] p = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[] ::new); int[] ints = new int[count + 1]; for (int i = 0; i < p.length; i++) { for (int j = p[i]; j <=count; j++) { ints[j] = Math.max(ints[j],ints[j-i]+p[i]); } } System.out.println(ints[count]); } }