Beruflich Dokumente
Kultur Dokumente
57m
IIT Guwahati
to test end
Lead a Life
You are working in Samara, Russia for a few days. Each day has a new pay per unit of work and a
- Question 1 - new cost per unit of food. Working 1 unit costs 1 unit of energy, and eating 1 unit of food adds 1
unit of energy. Here are some specifications of your employment:
1
You arrive with no money, but with energy. You can never have more energy than you arrive
- Question 2 - with, and it can never be negative.
You can do any amount of work every day (possibly not do any work at all), limited only by
2
your energy. You cannot work when your energy is zero.
You can eat any amount of food every day (possibly not have any food at all), limited by the
money you have. You cannot eat when the money you have is zero.
You can eat food at the end of the day, and cannot return to work after eating. You can return
to work on the next day.
Your true goal is to return home with as much money as possible. Compute the maximum
amount of money you can take home.
For example, consider a 3 day stay where pay per unit work for each day is as follows: earning=[1,
2, 4]. The cost of food is cost=[1, 3, 6]. You start with e=5 units of energy.
First day: 1 unit work is worth 1, and 1 unit food costs 1. There is no financial incentive to go
to work this day.
Second day: 1 unit work earns 2, and 1 unit food costs 3. Thus you spend more to eat than
total earning so there is no financial incentive to go to work on this day.
Third day: You earn 4 units per unit of work. The cost of food is irrelevant this day, as you are
leaving for the home straight from work. You spend all of your energy working, collect your
pay: 5 × 4 = 20 units of money and go home without buying dinner.
Function Description
Complete the function calculateProfit in the editor below. The function must return an integer
that represents the maximum earnings that can be taken home at the end of your stay.
calculateProfit has the following parameter(s):
n: the number of days
earning[earning[0],...earning[n-1]]: the daily pay per unit work
cost[cost[0],...cost[n-1]]: the daily cost per unit food
e: starting units of energy
Constraints
1 ≤ n ≤ 100
1 ≤ e ≤ 15
1 ≤ earning[i] ≤ 100
1 ≤ cost[i] ≤ 5
2
2
1
2
2
- Question 1 - 1
4
1 5
- Question 2 -
Sample Output 0
2
10
Explanation 0
n=2
earning = [1,2]
cost = [1,4]
e=5
You skip work on the first day and work with all of your energy on the last day. On day 2 you
have e=5 energy units to start the day and work is worth 2 units pay. At the end of the last day,
you collect your pay and go home without eating and with no energy.
Your take-home pay is 5 × 2 = 10 units.
Sample Case 1
Sample Case 2
YOUR ANSWER
We recommend you take a quick tour of our editor before you proceed. The timer will ✖
pause up to 90 seconds for the tour. Start tour
1 ▸ #include ↔
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7
8
9 ▾ /*
10 * Complete the 'calculateProfit' function below.
11 *
12 * The function is expected to return an INTEGER.
57m
13 * Guwahati
IIT The function accepts following parameters:
to test end
14 * 1. INTEGER n
15 * 2. INTEGER_ARRAY earning
16 * 3. INTEGER_ARRAY cost
17 * 4. INTEGER e
18 */
19
- Question 1 -
20 ▾ int calculateProfit(int n, vector<int> earning, vector<int> cost,
int e) {
1 21
22 }
- Question 2 -
23
24
2
25 int main()
26 ▾ {
27 ofstream fout(getenv("OUTPUT_PATH"));
28
29 string n_temp;
30 getline(cin, n_temp);
31
32 int n = stoi(ltrim(rtrim(n_temp)));
33
34 string earning_count_temp;
35 getline(cin, earning_count_temp);
36
37 int earning_count = stoi(ltrim(rtrim(earning_count_temp)));
38
39 vector<int> earning(earning_count);
40
41 ▾ for (int i = 0; i < earning_count; i++) {
42 string earning_item_temp;
43 getline(cin, earning_item_temp);
44
45 int earning_item = stoi(ltrim(rtrim(earning_item_temp)));
46
47 ▾ earning[i] = earning_item;
48 }
49
50 string cost_count_temp;
51 getline(cin, cost_count_temp);
52
53 int cost_count = stoi(ltrim(rtrim(cost_count_temp)));
54
55 vector<int> cost(cost_count);
56
57 ▾ for (int i = 0; i < cost_count; i++) {
58 string cost_item_temp;
59 getline(cin, cost_item_temp);
60
61 int cost_item = stoi(ltrim(rtrim(cost_item_temp)));
62
63 ▾ cost[i] = cost_item;
64 }
65
57m
66 IIT Guwahati
string e_temp;
to test end
67 getline(cin, e_temp);
68
69 int e = stoi(ltrim(rtrim(e_temp)));
70
71 int result = calculateProfit(n, earning, cost, e);
72
- Question 1 -
73 fout << result << "\n";
74
1 75 fout.close();
76
- Question 2 -
77 return 0;
78 }
2
79
80 ▾ string ltrim(const string &str) {
81 string s(str);
82
83 s.erase(
84 s.begin(),
85 find_if(s.begin(), s.end(), not1(ptr_fun<int, int>
(isspace)))
86 );
87
88 return s;
89 }
90
91 ▾ string rtrim(const string &str) {
92 string s(str);
93
94 s.erase(
95 find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>
(isspace))).base(),
96 s.end()
97 );
98
99 return s;
100 }
101
Line: 18 Col: 1
Test against custom input Run Code Submit code & Continue
Download sample test cases The input/output files have Unix line endings. Do not use Notepad to edit
them on windows.