在两个List<>上操作

时间:2016-05-05 16:25:34

标签: c# algorithm

我坚持了很长一段时间。我希望我的程序能做什么:

我将有两个清单。数量和价格之一。我想根据他们的序列乘以两个(例如:quantity [i] * price [i])并将乘法结果加在一起并得到一个特定的数字让我说我添加它们然后我得到100但我想要101.123和我的方式想实现的是在第一个价格数字上加0.001(我不能触及数量)并检查它是否与我想要的答案相符。我不能添加超过5,所以如果它无法从第一个数字中获取数字我想要移动到第二个数字并保留第一个数字,就像之前一样。任何人?这是我得到的地方。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ultimateproject_beginner_ {
    class Program {
        static List<decimal> QuantityList() {
            Console.WriteLine("Quantity");
            Console.WriteLine();
            List<decimal> quantityList = new List<decimal>();
            for (;;) {
                string stringQuantityNumber = Console.ReadLine();
                decimal quantityNumber = 0M;
                if (stringQuantityNumber == "done") {
                    break;
                } else {
                    if (decimal.TryParse(stringQuantityNumber, out quantityNumber)) {
                        quantityList.Add(quantityNumber);
                    }
                }
            }//end of for loop
            return quantityList;
        }

        static List<decimal> PriceList() {
            Console.WriteLine("Price");
            List<decimal> priceList = new List<decimal>();
            for (;;) {
                string stringPriceNumber = Console.ReadLine();
                decimal priceNumber = 0M;
                if (stringPriceNumber == "done") {
                    break;
                } else {
                    if (decimal.TryParse(stringPriceNumber, out priceNumber)) {
                        priceList.Add(priceNumber);

                    }
                }
            }//end of for loop
            return priceList;
        }

        static void Main(string[] args) {
            List<decimal> quantityList = QuantityList();
            List<decimal> priceList = PriceList();
            decimal destination = 101.123M;
            decimal sum = 0M;
            for (int i = 0; i < quantityList.Count; i++) {
                decimal product = priceList[i] * quantityList[i];
                sum = sum + product;
            }
            Console.ReadKey(true);
        }
    }
}

我试图让它与一些嵌套的for循环一起工作,但是我遇到了必须将新值与所有其他值相乘的地方。

我得到的:100,我想要的:101.123如何:通过向priceList添加0.001并检查总和是否为101.123

2 个答案:

答案 0 :(得分:1)

处理此问题的一种方法是计算第一条路径上的总数,然后确定需要多少次添加,然后执行调整。请注意,您可能无法达到所需目标,因为您可以添加的最大值受限于所有订购商品总数的0.05倍。如果总和为100美元,则需要101.23美元,但订单总共只有10件,每件0.04美元的最高价为100.50美元。

您可以使用LINQ计算总数:

var total = quantityList.Zip(priceList, (q, p) => q*p).Sum();

计算要分配的剩余值,并浏览各行,然后进行调整,直到remaining降至零:

var remaining = destination - total;
for (int i = 0; remaining > 0 && i < quantityList.Count; i++) {
    var quantity = quantityList[i];
    // Avoid crashes on zero quantity
    if (quantity == 0) {
        continue;
    }
    // We cannot assign more than quantity * 0.05
    var toAssign = Math.Min(remaining, quantity * 0.05);
    remaining -= toAssign;
    // Split the amount being assigned among the items
    priceList[i] += toAssign / quantity;
}
if (remaining > 0) {
    Console.WriteLine("Unable to distribute {0:C}", remaining);
}

注意:理想情况下,您应该考虑创建一个代表数量/价格对的类,而不是使用parallel lists

答案 1 :(得分:0)

计算总和后,将其与目的地进行比较。如果总和&lt;目的地,然后计算所需的0.001增量的数量= count =(destination - sum)/ 0.001。如果这小于5,则将这多个添加到第一个价格,否则添加其中五个(0.005),然后从计数中减去5。如果计数&gt; 0然后重复第二个价格的过程,等等。如果count大于pricelist.Count * 5,则无法达到目的地价格。