如何计算定制车的价格(条件是同一部件不能在同一辆车上添加两次)

How to calculate custom car prices (with the condition, that the same component is not added twice to the same car)

我有一个计算汽车价格的控制台应用程序。基本配置成本为 1000 美元,可以通过额外的 6 个选项进行扩展(可以在代码的注释中找到)。每台机器都有一个唯一的 ID。机器可以涂上一种或多种颜色。

注意! 必须选择附加选项(没有选项根本无法计算基本配置)。同一辆车不允许重复两次相同的组件。

输入: ID(第 1 个位置)6 个选项中的 1 个(2 -nd pos) ... 至 -1.

输出: 按汽车 ID 升序排列的汽车价格 ... 到 -1

在此处查看更多信息:Calculation price table for ids and options (for custom auto configuration)

我的解决方案:

using System;
using System.Collections.Generic;
using System.Linq;

namespace CarFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            // input numbers
            var numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            // step
            int i = 0;

            List<Car> cars = new List<Car>();

            // check while step not equils termination number
            while (numbers[i] != -1)
            {
                // check if value == null
                // then, create an item with unique ID
                // and add one or more options for car configuration
                if (cars.Where(x => x.id == numbers[i]).FirstOrDefault() == null)
                {
                    Car c = new Car();
                    c.id = numbers[i];
                    c.cDetails[numbers[i + 1] - 1] = true;
                    cars.Add(c);
                }
                else
                {
                    // if not null
                    Car c = cars.Where(x => x.id == numbers[i]).FirstOrDefault();
                    c.cDetails[numbers[i + 1] - 1] = true;
                }

                i += 2; // increase step by 2
            }

            // sorting car prices in ascending order of car ids
            var sortedCarId = from x in cars orderby x.id select x;

            foreach (var carPrice in sortedCarId)
            {
                Console.Write(carPrice.GetCarPrice() + " ");
            }
            Console.Write(-1);
        }
    }

    class Car
    {
        public int id;
        public bool[] cDetails = new bool[6];

        // calculate the cost of each car with custom configuration
        public int GetCarPrice()
        {
            int[] el = new int[6];
            el[0] = Convert.ToInt32(cDetails[0]) * 150;     // Air conditioning for 0
            el[1] = Convert.ToInt32(cDetails[1]) * 50;      // Power windows for 
            el[2] = Convert.ToInt32(cDetails[2]) * 125;     // Parking assistance for 5
            el[3] = Convert.ToInt32(cDetails[3]) * 25;      // Black paint for 
            el[4] = Convert.ToInt32(cDetails[4]) * 20;      // Green paint for 
            el[5] = Convert.ToInt32(cDetails[5]) * 30;      // Pink paint for 

            // basic car configuration costs 00 + sum of options
            return el.Sum() + 1000;
        }
    }
}

// Input:   1 2 1 1 2 2 1 5 2 5 3 1 3 6 2 4 3 3 5 1 -1
// Output:  1220 1095 1305 1150 -1

程序计数不错,但并非所有测试都通过:只有 6 / 10。 我有 4 个案例 - 结果错误。

为什么这段代码没有通过所有测试用例?我不知道。帮助任何人,算法或代码)谢谢大家!

C# 上的代码解决方案,10/10

using System;
using System.Collections.Generic;
using System.Linq;

namespace CarFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();

            Console.Write(String.Join(" ", BlackBox(numbers)) + " " + -1);
        }

        private static int[] BlackBox(int[] numbers)
        {
            Dictionary<int, int> carPrices = new Dictionary<int, int>();
            carPrices.Add(0, 0);
            carPrices.Add(1, 150);
            carPrices.Add(2, 50);
            carPrices.Add(3, 125);
            carPrices.Add(4, 25);
            carPrices.Add(5, 20);
            carPrices.Add(6, 30);

            Dictionary<int, int> totals = new Dictionary<int, int>();

            for (int i = 0; i < numbers.Length; i += 2)
            {
                int key = numbers[i];

                if (key == -1)
                    break;

                int priceKey = numbers[i + 1];

                if (totals.ContainsKey(key))
                    totals[key] += carPrices[priceKey];
                else
                    totals[key] = carPrices[priceKey];
            }
            return new SortedDictionary<int, int>(totals)
                .Select(e => 1000 + e.Value)
                .TakeWhile(x => x != -1)
                .ToArray();
        }
    }
}