I want to complete a program where I buy multiple items in bulk and buy them at a discount.

Asked 1 years ago, Updated 1 years ago, 50 views

I would like to complete a program where I can buy several items in bulk and buy them at a discount on Java.

It's a program that outputs standard output from standard input values, but it's struggling.Could you help me?

The challenges are as follows:

There are N kinds of products.In this campaign, if you buy s_i pieces of p_i yen product i in bulk, you will get a discount of d_i yen from the payment amount.You will be given the purchase information of K units (the number c_j and quantity a_j of the purchased item), so please ask for the payment amount after each discount.

For example, the unit price of product 1 (apple) is 20 yen, the unit price of 5 pieces is 10 yen, the unit price of product 2 (peaman) is 45 yen, and the unit price of 6 pieces is 40 yen.

If the purchase details are as follows, the payment amount will be as follows.

Purchase 6 units of item 1
→ Since 6 55=1 or so, you can get a discount once, and the payment is 20×6-10×1=110 yen

Purchase 12 units of item 2
→ 12 66=2, so you can apply the discount twice. Payment is 45×12-40×2=460 yen

Standard input values are as follows:

Inputs will be given in the following format in standard input

N
p_1s_1d_1
p_2s_2d_2
...
p_Ns_Nd_N
M
c_1 a_1
c_2a_2
...
c_Ma_M

·In the first line, an integer N representing the total number of types of products to be campaigned on is given.
·In line i (1 ii NN) of the following N lines, an integer p_i representing the unit price of item i and two integers s_i, d_i representing the details of the campaign are given in this order, separated by half-width spaces.
 ·This means that you will get a discount of d_i yen if you buy s_i items in bulk.
·The next line is given an integer M representing the total number of purchases entered.
·In the next M line, line j (1 )j MM) is given an integer c_j representing the number of the purchased item and an integer a_j representing the number of purchases of the item in this order, separated by half-width spaces.
·The total input is N+M+2 lines, and a new line is added to the end of the last line of the input value.

Example Input 1
Input

2
20 5 10
45 6 40
2
1 6
2 12

Output

110
460

Example Input 2
Input

4
30 2 10
55 5 40
100 1 2
25 10 25
6
1 1
2 1
3 3
1 3
2 9
4 52

Output

30
55
294
80
455
1175

This is the code that I implemented halfway.You don't have to use the code because it's shown as something you've done.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class kiesan {

    public static void main(String[]args) {

        Scanner sc = new Scanner (System.in);
        List<String>list=new ArrayList<String>();;
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            list.add(str);
        }

        List<String>priceList=new ArrayList<String>();
        for(inti=1;i<=Integer.valueOf(list.get(0));i++){
            Strings=list.get(i);
            priceList.add(s);
        }

        for(inti=4;i<(i+Integer.valueOf(list.get(3));i++){
            Strings=list.get(i);
            // That's all I could do.
        }

    }

}

I would like to implement a program that results in input examples 1 and 2.

java

2022-09-30 21:48

1 Answers

import java.util.*;

public class Main {
    public static void main(String[]args) {

        Scanner sc = new Scanner (System.in);
        int n = Integer.parseInt(sc.next());
        int[]p = new int[n];
        int[]s = new int[n];
        int[]d = new int[n];

        for(inti=0;i<n;i++){
            p[i] = Integer.parseInt(sc.next());
            s[i] = Integer.parseInt(sc.next());
            d[i] = Integer.parseInt(sc.next());
        }

        intm = Integer.parseInt(sc.next());

        for(inti=0;i<m;i++){
            intc=Integer.parseInt(sc.next())-1;
            inta = Integer.parseInt(sc.next());
            System.out.println(a*p[c]-(a/s[c])*d[c]);
        }
    }
}


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.