📌  相关文章
📜  给定类型的所有项目的最低、最高和平均价格值

📅  最后修改于: 2022-05-13 01:57:08.444000             🧑  作者: Mango

给定类型的所有项目的最低、最高和平均价格值

给定一个字符串数组item[]和一个整数数组price[] ,其中price[i]是从第i商店购买时item[i]的价格。任务是找到所有购买物品的最低、最高和平均价格值。

例子:

方法:创建一个 HashMap,其中项目的名称将是一个字符串,值将是一个对象,该对象将为每个项目存储四种类型的值,即

  1. min:存储当前类型项目的最小值。
  2. max:存储当前类型项目的最大值。
  3. total:当前类型的总项。
  4. sum:当前类型商品的价格总和。

现在,对于存储在 Hashmap 中的每个项目,最小和最大价格存储在 value 对象中,平均值可以计算为sum / total

下面是上述方法的实现:

Java
// Java implementation of the approach
import java.util.*;
class GFG {
  
    // To store an item
    static class Item {
  
        // To store the minimum and the
        // maximum price for the item
        int min, max;
  
        // To store the total number of items
        // of the current type and the
        // total cost of buying them
        int total, sum;
  
        // Initializing an element
        Item(int price)
        {
            min = price;
            max = price;
            total = 1;
            this.sum = price;
        }
    }
  
    // Function to find the minimum, the maximum
    // and the average price for every item
    static void findPrices(String item[], int price[], int n)
    {
  
        // To store the distinct items
        HashMap map = new HashMap<>();
  
        // For every item
        for (int i = 0; i < n; i++) {
  
            // If the current item has aready been
            // purchased earlier from a different shop
            if (map.containsKey(item[i])) {
  
                // Get the item
                Item currItem = map.get(item[i]);
  
                // Update its minimum and maximum price so far
                currItem.min = Math.min(currItem.min, price[i]);
                currItem.max = Math.max(currItem.max, price[i]);
  
                // Increment the total count of the current item
                currItem.total++;
  
                // Add the current price to the sum
                currItem.sum += price[i];
            }
            else {
  
                // The item has been purchased for the first time
                Item currItem = new Item(price[i]);
                map.put(item[i], currItem);
            }
        }
  
        // Print all the items with their
        // minimum, maximum and average prices
        System.out.println("Item Min Max Average");
        for (Map.Entry ob : map.entrySet()) {
            String key = ob.getKey();
            Item currItem = ob.getValue();
            System.out.println(key + " " + currItem.min
                               + " " + currItem.max + " "
                               + ((float)currItem.sum / (float)currItem.total));
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        String item[] = { "toy", "pen", "notebook", "pen" };
        int n = item.length;
        int price[] = { 2, 1, 3, 2 };
  
        findPrices(item, price, n);
    }
}


C#
// C# implementation of the approach 
using System;         
using System.Collections.Generic;
  
class GFG 
{
  
    // To store an item
    public class Item
    {
  
        // To store the minimum and the
        // maximum price for the item
        public int min, max;
  
        // To store the total number of items
        // of the current type and the
        // total cost of buying them
        public int total, sum;
  
        // Initializing an element
        public Item(int price)
        {
            min = price;
            max = price;
            total = 1;
            this.sum = price;
        }
    }
  
    // Function to find the minimum, the maximum
    // and the average price for every item
    static void findPrices(String []item, 
                              int []price, int n)
    {
  
        // To store the distinct items
        Dictionary map = new Dictionary();
  
        // For every item
        for (int i = 0; i < n; i++)
        {
  
            // If the current item has aready been
            // purchased earlier from a different shop
            if (map.ContainsKey(item[i]))
            {
  
                // Get the item
                Item currItem = map[item[i]];
  
                // Update its minimum and
                // maximum price so far
                currItem.min = Math.Min(currItem.min,
                                           price[i]);
                currItem.max = Math.Max(currItem.max, 
                                           price[i]);
  
                // Increment the total count of 
                // the current item
                currItem.total++;
  
                // Add the current price to the sum
                currItem.sum += price[i];
            }
            else 
            {
  
                // The item has been purchased
                // for the first time
                Item currItem = new Item(price[i]);
                map.Add(item[i], currItem);
            }
        }
  
        // Print all the items with their
        // minimum, maximum and average prices
        Console.WriteLine("Item Min Max Average");
        foreach(KeyValuePair ob in map)
        {
            String key = ob.Key;
            Item currItem = ob.Value;
            Console.WriteLine(key + " " + currItem.min + 
                                    " " + currItem.max + 
                                    " " + ((float)currItem.sum /
                                           (float)currItem.total));
        }
    }
  
    // Driver code
    public static void Main(String []args)
    {
        String []item = { "toy", "pen", "notebook", "pen" };
        int n = item.Length;
        int []price = { 2, 1, 3, 2 };
  
        findPrices(item, price, n);
    }
}
  
// This code is contributed by 29AjayKumar


输出:
Item Min Max Average
pen 1 2 1.5
toy 2 2 2.0
notebook 3 3 3.0