Team topic - shopping plan

The task of this level: the company issued a shopping voucher of 1,000 yuan in a certain store, and it is limited to only buy m products in the store. The prices of each commodity are: n1, n2,...; the program is required to list all the different shopping methods that can just consume the shopping coupon.
Program input:
The first line is an integer m, which represents the number of types of goods that can be purchased.
Next are m integers, each line represents the unit price of m commodities.
Program output:
the first line is an integer, indicating how many plans there are in total
. From the second line, each plan occupies 1 line, indicating the quantity of each commodity purchased, separated by spaces.

related information

In order to complete the task of this level, you need to master: 1. How to divide and conquer recursively

programming requirements

According to the prompt, supplement the code in the editor on the right

Test instruction

The platform will test the code you write:

Test input:
2 200
300
Expected output:
2 2 2
5 0

Test input:
2 500
800
Expected output:
1 2 0

hint:

 
 
  1. public void dfs(int sum, int step) {
  2. if(step == m) { //如果已达到最后一个商品
  3. if(sum == 1000) { //如果钱正好花完
  4. }
  5. return;
  6. } else {
  7. }
  8. }
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class Main {
        public static ArrayList<Integer> list = new ArrayList<Integer>();   // 创建一个ArrayList对象
        public static int[][] value;
        public static int m;
        public static int count = 0;
        public static String result = "";
    
        public void dfs(int sum, int step) {
            if(step == m){//如果已到达最后一个商品
                    if(sum==1000) {//如果钱正好花完
                        for (int i = 0; i < list.size(); i++) {
                            result += list.get(i) + "";
                        }
                        result += "\n";
                        count++;
                    }
                            return;
                        }
                        else{
                            for(int i=0;i<=value[step][1];i++){
                                sum+=value[step][0]*i;
                                list.add(i);
                                dfs(sum,step+1);
                                sum-=value[step][0]*i;
                                list.remove(list.size()-1);
                        }
            }  
        }
    
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            m = in.nextInt();
            value = new int[m][2];
            for(int i = 0;i < m;i++) {
                int a = in.nextInt();
                int num=1000/a;
                value[i][0]=a;
                value[i][1]=num;
            }
            test.dfs(0, 0);
            if(count == 0)
                System.out.println("0");
            else
                System.out.println(count+"\n"+result);
        }
    }

     

Guess you like

Origin blog.csdn.net/foxmaxiaoyu/article/details/124724191