Waste less time on Facebook — follow Brilliant.
×

Programming Task 2: Cashier.

Hey guys, here is task number \(\boxed{2}\)!

As you can see, the name of this is CASHIER.

Plarry the Dinosaur has finally found a job as a cashier! (yay) However, the thing is, he is terrible at mental sum and wants you to write a code to help him in calculating the cost of items.

Input Details:

First line inputs n.

Next n lines input the cost of the ith item.

This line inputs x.

The next x lines are the items the purchaser bought, the number i representing the ith item.

Output Details:

The total cost of all the items.

Sample input:

3

0.25

5.67

10.01

2

3

1

Sample output:

10.26

Explanation for output:

The 1st item costs 0.25 dollars and the 3rd item costs 10.01 dollars. Thus, the total cost is 10.26 dollars.

Note by Charlton Teo
2 years, 9 months ago

No vote yet
1 vote

Comments

Sort by:

Top Newest

My solution in

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
using namespace std;

int main(){

    int items; cin >> items;

    vector <double> costs;
    while (items--){
        double cost; cin >> cost;
        costs.push_back(cost);
    }

    int bought; cin >> bought;

    double sum = 0;
    while (bought--){
        int index; cin >> index;
        sum += costs[index];
    }

    cout << sum;

    return 0;
}
Daniel Lim · 2 years, 9 months ago

Log in to reply

My solution in

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;

public class Cashier {

    static Scanner in = new Scanner(System.in);

    public static void main(String args[]) {

        int items = in.nextInt();

        double[] costs = new double[items];
        for (int i = 0; i < items; i++) {
            costs[i] = in.nextDouble();
        }

        int bought = in.nextInt();

        double sum = 0;
        for (int i = 0; i < bought; i++) {
            int index = in.nextInt();

            sum+=costs[index - 1];
        }

        System.out.println(sum);
    }
}
Daniel Lim · 2 years, 9 months ago

Log in to reply

And here is python2 solution below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
num = input() # input n
price_list = []

# inserting price for ith item
for n in xrange(num):
    price_list.append(raw_input())

items = input() # no of item bought 
cost = 0

# summing up prices
for item in xrange(items):
    cost += float(price_list[input()-1]) 
print cost
Iamsudip Iamsudip · 2 years, 9 months ago

Log in to reply

sorry ur the only one who tried this daniel XD anyway my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
   int x,n;
   double p[1000000];
   double b = 0;
   cin>>x;
   for(int i = 0; i < x; i++){
       cin>>p[i];
   }
   cin>>n;
   for(int i = 0; i < n; i++){
       int a;
       cin>>a;
       b+=p[a-1];
   }
   cout<<b;
}
Charlton Teo · 2 years, 9 months ago

Log in to reply

@Charlton Teo maybe no one saw this post Daniel Lim · 2 years, 9 months ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...