📜  导致生活问题

📅  最后修改于: 2021-05-18 00:46:52             🧑  作者: Mango

您正在俄罗斯萨马拉(Samara)工作几天。每天都有每单位工作的新工资和每单位食物的新费用。工作1单位需要1单位能量,而吃1单位食物则要增加1单位能量。以下是您工作的一些规格:

  • 您抵达时一无所有,但精力充沛。
  • 您永远不会拥有比到达的能量更多的能量,而且它永远不会是负面的。
  • 您每天可以做任何数量的工作(可能根本不做任何工作),仅受您的精力限制。
  • 当您的能量为零时,您将无法工作。您每天可以吃任何数量的食物(可能根本没有任何食物),这取决于您所拥有的资金。
  • 当您的钱为零时,您将无法吃饭。您可以在一天结束时吃东西,也不能在吃完饭后重返工作岗位。
  • 您第二天可以重新上班。

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

例子:

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

  1. 对于收入[]成本[]中的每个元素,请将收入[i]成本[i]进行比较
  2. 如果收入[i]小于或等于cost [i] ,则当费用成本大于收入时,请跳过当天的工作。因此,根本不赚钱。
  3. 如果Earning [i]大于cost [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


输出:
70

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