📜  解决生活问题

📅  最后修改于: 2021-10-25 11:23:39             🧑  作者: Mango

你在俄罗斯萨马拉工作了几天。每一天都有一个新的每单位工作工资和一个新的每单位食物成本。工作 1 单位消耗 1 单位能量,吃 1 单位食物增加 1 单位能量。以下是您的工作的一些规格:

  • 你到达时没有钱,但有能量。
  • 你的能量永远不会超过你到达时的能量,它永远不会是消极的。
  • 您每天可以做任何数量的工作(可能根本不做任何工作),仅受您的精力限制。
  • 当你的能量为零时,你无法工作。您每天可以吃任意数量的食物(可能根本没有任何食物),这受您拥有的金钱的限制。
  • 当你的钱为零时,你不能吃饭。您可以在一天结束时进食,进食后不能返回工作。
  • 第二天就可以上班了。

你的真正目标是带着尽可能多的钱回家。计算您可以带回家的最大金额。

例子:

方法:方法是遍历给定的收益数组和成本数组,计算每一天的净利润。以下是步骤:

  1. 对于earning []cost[] 中的每个元素,将earning[i]cost[i] 进行比较
  2. 如果Earning[i] 小于或等于 cost[i] ,则跳过当天的工作,因为费用成本大于收入。因此,根本没有盈利。
  3. 如果收入[i] 大于成本[i] ,则通过将当天的收入乘以总能量单位来计算总收入,然后减去当天的食物成本和能量单位的乘积来计算利润那天。
  4. 如果有最后一个工作日,那么通过将当天的收入乘以总能量单位来计算总收入,这将是您当天的利润,因为工作不再需要能量。
  5. 完成上述步骤后打印总利润。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that calculates the profit
// with the earning and cost of expenses
int calculateProfit(int n, int* earnings,
                    int* cost, int e)
{
    // To store the total Profit
    int profit = 0;
 
    // Loop for n number of days
    for (int i = 0; i < n; i++) {
 
        int earning_per_day = 0;
        int daily_spent_food = 0;
 
        // If last day, no need to buy food
        if (i == (n - 1)) {
            earning_per_day = earnings[i] * e;
            profit = profit + earning_per_day;
            break;
        }
 
        // Else buy food to gain
        // energy for next day
        if (cost[i] < earnings[i]) {
 
            // Update earning per day
            earning_per_day = earnings[i] * e;
            daily_spent_food = cost[i] * e;
 
            // Update profit with daily spent
            profit = profit + earning_per_day
                     - daily_spent_food;
        }
    }
 
    // Print the profit
    cout << profit << endl;
}
 
// Driver Code
int main()
{
    // Given days
    int n = 4;
 
    // Given earnings
    int earnings[] = { 1, 8, 6, 7 };
 
    // Given cost
    int cost[] = { 1, 3, 4, 1 };
 
    // Given energy e
    int e = 5;
 
    // Function Call
    calculateProfit(n, earnings, cost, e);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function that calculates the profit
// with the earning and cost of expenses
static void calculateProfit(int n, int []earnings,
                            int []cost, int e)
{
     
    // To store the total Profit
    int profit = 0;
 
    // Loop for n number of days
    for(int i = 0; i < n; i++)
    {
        int earning_per_day = 0;
        int daily_spent_food = 0;
 
        // If last day, no need to buy food
        if (i == (n - 1))
        {
            earning_per_day = earnings[i] * e;
            profit = profit + earning_per_day;
            break;
        }
 
        // Else buy food to gain
        // energy for next day
        if (cost[i] < earnings[i])
        {
 
            // Update earning per day
            earning_per_day = earnings[i] * e;
            daily_spent_food = cost[i] * e;
 
            // Update profit with daily spent
            profit = profit + earning_per_day -
                              daily_spent_food;
        }
    }
 
    // Print the profit
    System.out.print(profit + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given days
    int n = 4;
 
    // Given earnings
    int earnings[] = { 1, 8, 6, 7 };
 
    // Given cost
    int cost[] = { 1, 3, 4, 1 };
 
    // Given energy e
    int e = 5;
 
    // Function call
    calculateProfit(n, earnings, cost, e);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
 
# Function that calculates the profit
# with the earning and cost of expenses
def calculateProfit(n, earnings, cost, e):
     
    # To store the total Profit
    profit = 0;
 
    # Loop for n number of days
    for i in range(n):
        earning_per_day = 0;
        daily_spent_food = 0;
 
        # If last day, no need to buy food
        if (i == (n - 1)):
            earning_per_day = earnings[i] * e;
            profit = profit + earning_per_day;
            break;
 
        # Else buy food to gain
        # energy for next day
        if (cost[i] < earnings[i]):
             
            # Update earning per day
            earning_per_day = earnings[i] * e;
            daily_spent_food = cost[i] * e;
 
            # Update profit with daily spent
            profit = (profit + earning_per_day -
                               daily_spent_food);
 
    # Print the profit
    print(profit);
 
# Driver Code
if __name__ == '__main__':
     
    # Given days
    n = 4;
 
    # Given earnings
    earnings = [ 1, 8, 6, 7 ];
 
    # Given cost
    cost = [ 1, 3, 4, 1 ];
 
    # Given energy e
    e = 5;
 
    # Function call
    calculateProfit(n, earnings, cost, e);
 
# This code is contributed by PrinciRaj1992


C#
// C# program for the above approach
using System;
class GFG{
 
// Function that calculates the profit
// with the earning and cost of expenses
static void calculateProfit(int n, int []earnings,
                            int []cost, int e)
{
     
    // To store the total Profit
    int profit = 0;
 
    // Loop for n number of days
    for(int i = 0; i < n; i++)
    {
        int earning_per_day = 0;
        int daily_spent_food = 0;
 
        // If last day, no need to buy food
        if (i == (n - 1))
        {
            earning_per_day = earnings[i] * e;
            profit = profit + earning_per_day;
            break;
        }
 
        // Else buy food to gain
        // energy for next day
        if (cost[i] < earnings[i])
        {
 
            // Update earning per day
            earning_per_day = earnings[i] * e;
            daily_spent_food = cost[i] * e;
 
            // Update profit with daily spent
            profit = profit + earning_per_day -
                              daily_spent_food;
        }
    }
 
    // Print the profit
    Console.Write(profit + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given days
    int n = 4;
 
    // Given earnings
    int []earnings = { 1, 8, 6, 7 };
 
    // Given cost
    int []cost = { 1, 3, 4, 1 };
 
    // Given energy e
    int e = 5;
 
    // Function call
    calculateProfit(n, earnings, cost, e);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
70

时间复杂度: O(N),其中 N 是天数
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程