📜  计算以折扣价出售给定项目所产生的损失

📅  最后修改于: 2021-05-04 09:17:02             🧑  作者: Mango

卖家想以X%的折扣出售他的物品。他将每件商品的价格提高了原始价格的X% 。任务是计算出售所有物品后产生的总损失。

例子:

方法:对于每个项目,计算其售价,即原始价格+原始价格的X%,然后将折扣价计算为销售价格–出售价格的X% 。现在,损失可以计算为(原始价格–折后价格)*数量。加上所有项目造成的损失,这是必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the x% of n
float percent(int n, int x)
{
    float p = n * x;
    p /= 100;
    return p;
}
  
// Function to return the total loss
float getLoss(int price[], int quantity[], int X[], int n)
{
    // To store the total loss
    float loss = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Original price of the item
        float originalPrice = price[i];
  
        // The price at which the item will be sold
        float sellingPrice = originalPrice
                             + percent(originalPrice, X[i]);
  
        // The discounted price of the item
        float afterDiscount = sellingPrice
                              - percent(sellingPrice, X[i]);
  
        // Loss incurred
        loss += ((originalPrice - afterDiscount) * quantity[i]);
    }
  
    return loss;
}
  
// Driver code
int main()
{
    int price[] = { 20, 48, 200, 100 };
    int quantity[] = { 20, 48, 1, 1 };
    int X[] = { 0, 48, 200, 5 };
  
    // Total items
    int n = sizeof(X) / sizeof(X[0]);
    cout << getLoss(price, quantity, X, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
    // Function to return the x% of n
    static float percent(int n, int x)
    {
        float p = n * x;
        p /= 100;
        return p;
    }
  
    // Function to return the total loss
    static float getLoss(int price[], int quantity[], int X[], int n)
    {
        // To store the total loss
        float loss = 0;
  
        for (int i = 0; i < n; i++) {
  
            // Original price of the item
            float originalPrice = price[i];
  
            // The price at which the item will be sold
            float sellingPrice = originalPrice
                                 + percent((int)originalPrice, X[i]);
  
            // The discounted price of the item
            float afterDiscount = sellingPrice
                                  - percent((int)sellingPrice, X[i]);
  
            // Loss incurred
            loss += ((originalPrice - afterDiscount) * quantity[i]);
        }
  
        return loss;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int price[] = { 20, 48, 200, 100 };
        int quantity[] = { 20, 48, 1, 1 };
        int X[] = { 0, 48, 200, 5 };
  
        // Total items
        int n = X.length;
        System.out.print(getLoss(price, quantity, X, n));
    }
}


Python3
# Python3 implementation of the approach
  
# Function to return the x% of n
def percent(n, x):
  
    p = (int)(n) * x;
    p /= 100;
    return p;
  
# Function to return the total loss
def getLoss(price, quantity, X, n):
  
    # To store the total loss
    loss = 0;
  
    for i in range(n):
  
        # Original price of the item
        originalPrice = price[i];
  
        # The price at which the item will be sold
        sellingPrice = originalPrice + percent(originalPrice, X[i]);
  
        # The discounted price of the item
        afterDiscount = sellingPrice - percent(sellingPrice, X[i]);
  
        # Loss incurred
        loss += ((originalPrice - afterDiscount) * quantity[i]);
  
    return round(loss,2);
  
# Driver code
price = [ 20, 48, 200, 100 ];
quantity = [ 20, 48, 1, 1 ];
X = [ 0, 48, 200, 5 ];
  
# Total items
n = len(X);
print(getLoss(price, quantity, X, n));
  
      
# This code is contributed by mits


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
// Function to return the x% of n 
static float percent(int n, int x) 
{ 
    float p = n * x; 
    p /= 100; 
    return p; 
} 
  
// Function to return the total loss 
static float getLoss(int []price, 
                     int []quantity, 
                     int []X, int n) 
{ 
    // To store the total loss 
    float loss = 0; 
  
    for (int i = 0; i < n; i++)
    { 
  
        // Original price of the item 
        float originalPrice = price[i]; 
  
        // The price at which the item will be sold 
        float sellingPrice = originalPrice + 
                percent((int)originalPrice, X[i]); 
  
        // The discounted price of the item 
        float afterDiscount = sellingPrice - 
                 percent((int)sellingPrice, X[i]); 
  
        // Loss incurred 
        loss += ((originalPrice - 
                  afterDiscount) * quantity[i]); 
    } 
  
    return loss; 
} 
  
// Driver code 
public static void Main() 
{ 
    int []price = { 20, 48, 200, 100 }; 
    int []quantity = { 20, 48, 1, 1 }; 
    int []X = { 0, 48, 200, 5 }; 
  
    // Total items 
    int n = X.Length; 
    Console.Write(getLoss(price, quantity, X, n)); 
} 
}
  
// This code is contributed by Ryuga


PHP


输出:
1330.17