c# - 对两个List<>的操作

标签 c# algorithm

我被这个困扰了很长一段时间。我希望我的程序做什么:

我将有两个列表。一种是数量,一种是价格。我想根据它们的序列号将两个相乘(例如:数量[i] * 价格[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 如何:将 0.001 添加到 priceList 并检查总和是否为 101.123

最佳答案

处理此问题的一种方法是计算第一条路径上的总数,然后计算出您需要添加多少,然后执行调整。请注意,您可能无法达到所需的目标,因为您可以添加的最大值限制为 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 .

关于c# - 对两个List<>的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37055570/

相关文章:

algorithm - 集合覆盖的近似

c# - 查询嵌套数据的有效方法

c# - 复杂类型 : multiple instances in one model?

c# - 将文件写入响应后关闭弹出窗口

javascript - C# 和 JavaScript 中的 B 树和稀疏索引算法

python - 计算到达目标的步行点数

c++ - 将 C++ 代码转换为 C 时程序崩溃

c# - 通过自定义异常处理传递额外的程序状态信息

c# - 填写所有要求字段后使用 Bootstrap Modal

python - 嵌入向量搜索高效算法