【面试题】购物单

王强今天很开心,公司发给N元的年终奖。王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子:
主件 附件
电脑 打印机,扫描仪
书柜 图书
书桌 台灯,文具
工作椅 无
如果要买归类为附件的物品,必须先买该附件所属的主件。每个主件可以有 0 个、 1 个或 2 个附件。附件不再有从属于自己的附件。王强想买的东西很多,为了不超出预算,他把每件物品规定了一个重要度,分为 5 等:用整数 1 ~ 5 表示,第 5 等最重要。他还从因特网上查到了每件物品的价格(都是 10 元的整数倍)。他希望在不超过 N 元(可以等于 N 元)的前提下,使每件物品的价格与重要度的乘积的总和最大。
设第 j 件物品的价格为 v[j] ,重要度为 w[j] ,共选中了 k 件物品,编号依次为 j 1 , j 2 ,……, j k ,则所求的总和为:
v[j 1 ]*w[j 1 ]+v[j 2 ]*w[j 2 ]+ … +v[j k ]*w[j k ] 。(其中 * 为乘号)
请你帮助王强设计一个满足要求的购物单。
输入的第 1 行,为两个正整数,用一个空格隔开:N m
(其中 N ( <32000 )表示总钱数, m ( <60 )为希望购买物品的个数。)
从第 2 行到第 m+1 行,第 j 行给出了编号为 j-1 的物品的基本数据,每行有 3 个非负整数 v p q
(其中 v 表示该物品的价格( v<10000 ), p 表示该物品的重要度( 1 ~ 5 ), q 表示该物品是主件还是附件。如果 q=0 ,表示该物品为主件,如果 q>0 ,表示该物品为附件, q 是所属主件的编号)

import java.util.*;
import java.util.stream.Collectors;

public class Main {
  static   List<Goods> goodsList;
    static   List<Goods> finna;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        String[] split = line.split("\\s+");
        //奖金
        int money=Integer.parseInt(split[0]);
        //物件数
        int num=Integer.parseInt(split[1]);
        goodsList=new LinkedList<>();
        finna=new LinkedList<>();
        //存入数据
        for(int i=0;i<num;i++){
            String nextLine = scanner.nextLine();
            String[] split1 = nextLine.split("\\s+");
           goodsList.add(
            new Goods(Integer.parseInt(split1[0]),
                              Integer.parseInt(split1[1]),
                              Integer.parseInt(split1[2]),
                    Integer.parseInt(split1[0])*Integer.parseInt(split1[1]),i+1)
           );
            finna.add(
                    new Goods(Integer.parseInt(split1[0]),
                            Integer.parseInt(split1[1]),
                            Integer.parseInt(split1[2]),
                            Integer.parseInt(split1[0])*Integer.parseInt(split1[1]),i+1)
            );
        }
        System.out.println(goodsList);
        //记录各路径钱用完后返回的价值
        List<Integer> integerList=new LinkedList<>();
        integerList.add(0,0);
        integerList.add(1,0);
        getMax(goodsList,money,integerList);
        System.out.println(integerList);
        integerList.remove(0);
        List<Integer> collect = integerList.stream().sorted().collect(Collectors.toList());
        System.out.println(collect.get(collect.size()-1));


    }
    static int getMax(List<Goods> goodsList,int money, List<Integer> integerList){
        int num=money;
        for(int i=0;i<goodsList.size();i++){
            int finalI = i;
            if(goodsList.get(i).q!=0){
                List<Goods> collect = goodsList.stream().filter(a -> a.code == goodsList.get(finalI).q).collect(Collectors.toList());
                if(collect.size()!=0){
                    continue;
                }
            }
            List<Goods> goodsList1=new LinkedList<>();
           goodsList.stream().forEach(a->goodsList1.add(a));
            num = num - goodsList.get(i).value;
            if(num<=0){
                integerList.add(integerList.get(0));
                continue;
            }

            integerList.add(0,integerList.get(0)+goodsList.get(i).total);
            goodsList1.remove(i);
            integerList.add(1,goodsList.get(i).code);
            if(goodsList1.size()==0){
                return 1;
            }
            getMax(goodsList1,num,integerList);
        }
        List<Goods> collect = finna.stream().filter(a -> a.code == integerList.get(1)).collect(Collectors.toList());
        System.out.println(finna);
        System.out.println(collect);
        Goods goods = collect.get(0);
        integerList.add(0,  integerList.get(0)-goods.value*goods.p);
        return 1;
    }
    static class Goods{
        int code;
        //价格
        int value;
        //物品的重要度(1-5)
        int p;
        //0为主件,非零为对应的主件编号
        int q;
        //价值:value*p
        int total;
        public Goods(int value,int p,int q,int total,int code){
            this.value=value;
            this.p=p;
            this.q=q;
            this.total=total;
            this.code=code;
        }

        @Override
        public String toString() {
            return "Goods{" +
                    "code=" + code +
                    ", value=" + value +
                    ", p=" + p +
                    ", q=" + q +
                    ", total=" + total +
                    '}';
        }
    }
}

原创文章 34 获赞 20 访问量 1137

猜你喜欢

转载自blog.csdn.net/qq_22744093/article/details/104724545
今日推荐