题目二(选做)
王师傅是一名卸货工人,现在有n个货物,由于王师傅一次可以同时卸2个货物,所以决定今天先卸其中的2m个货物。每次卸货物消耗的体力值计算公式为,假如2个货物的质量分别为x和y,消耗的体力值为(xy)的4次方,现给出n个货物分别的质量。求王师傅卸完2*m个货物后消耗的体力值是多少。
java代码实现
import java.util.Scanner;
public class EXER{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int[] productMass=new int[n];
for(int i=0;i<n;i++)
{
productMass[i]=sc.nextInt();
}
int res=consume(n, m, productMass);
System.out.println(res);
}
public static int consume(int n, int m, int[] arr){
int[][] src = new int[10][10];
int tmp = 0, sub = 0;
int MAX = 10000;
int i, j;
for (i = 0; i < n;){
for (j = 0; j <= m; j++){
if (j == 0){
src[i][j] = 0;
}
else if (i <= j * 2 - 2){
src[i][j] = MAX;
}
else{
if (i == 1){
src[i][j] = tmp;
}
else{
if ((tmp + src[i - 2][j - 1]) < src[i - 1][j]){
src[i][j] = tmp + src[i - 2][j - 1];
}
else{
src[i][j] = src[i - 1][j];
}
}
}
}
i++;
if(i<n) {
sub = arr[i] - arr[i - 1];
tmp = (int)Math.pow(sub, 4);
}
}
return src[n-1][m];
}
}
转载:https://blog.csdn.net/weixin_40169387/article/details/102550798
查看评论