【问题标题】:C++ Matching Doubles in an Array (With incremental number of entries in matching)C ++匹配数组中的双打(匹配中的条目数量增加)
【发布时间】:2015-01-10 18:37:57
【问题描述】:

早安,

我对 C++ 还很陌生。我有一个项目,我需要提出一个应用程序来进行匹配。

假设共有 100 件商品,每件商品都有不同的价格,存储在名为 PriceDB.txt 的文本文件中。

文本文件的结构:

Item1 3.99
Item2 9.99
Item3 11.88
Item4 87.10
Item5 5.69
Item6 13.00
Item7 245.22
... (and so on)

它(应该)是这样工作的:

  • c++ 应用程序将请求输入您希望匹配的价格
  • 首先将 2 件商品的价格(商品 1 和商品 1)相加,然后将总价与您的输入进行比较
  • 如果 Item1 和 Item1 的总价格与输入值不匹配,则继续添加 Item1 和 Item2 的价格,然后是 Item1 和 Item3,依此类推
  • 如果直到 Item1 和 Item100 组合后总数仍然不匹配,则继续添加 Item2 和 Item2、Item2 和 Item3、Item2 和 Item4 等等
  • 请注意,以上只有 2 个价格组合。当它到达 Item100 & Item100 并且仍然没有找到时,它继续以 3 个价格组合..
  • 例如Item1 & Item1 & Item1, Item1 & Item1 & Item2, 直到Item100 & Item100 & Item100,继续4个价格组合
  • 该过程将一直持续到达到 100 个价格组合。

我已经通过下面的代码部分实现了我想要的:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


bool double_equals(double a, double b, double epsilon = 0.01)
{
    return abs(a - b) < epsilon;
}

int main() {

    double PriceToMatch, ItemPrice[5];
    string ItemName[5];

    ifstream PriceDB("PriceDB.txt", ios::binary);

    if (!PriceDB.is_open()) {
        cout << "ERROR: Failed to read price database, exiting...";
        return 1;
    }

    for (int i = 0; !PriceDB.eof(); i++) {
        PriceDB >> ItemName[i];
        PriceDB >> ItemPrice[i];
    }

    cout << "Enter the price to match: ";
    cin >> PriceToMatch;


    for (int i = 0; i < 5; i++) {
        for (int x = i; x < 5; x++) {
            if (double_equals(ItemPrice[i] + ItemPrice[x], PriceToMatch) == true) {
                cout << "Found: " << ItemName[i] << " + " << ItemName[x] << endl;
            }
        }
    }

    for (int a = 0; a < 5; a++) {
        for (int b = a; b < 5; b++) {
            for (int c = b; c < 5; c++) {
                if (double_equals(ItemPrice[a] + ItemPrice[b] + ItemPrice[c], PriceToMatch) == true) {
                    cout << "Found: " << ItemName[a] << " + " << ItemName[b] << " + " << ItemName[c] << endl;
                }
            }
        }
    }

    return 0;
}

以上代码适用于 2 个价格组合和 3 个价格组合。

但是,我必须为组合中的更多价格添加更多 If/else 集。这将是一个非常大的麻烦,因为它会导致大量的代码页面。知道如何解决这个问题吗?

【问题讨论】:

  • 您可以使用存储为整数的美分,而不是使用浮点数。也就是说,减少这里的代码量。即使输入代码也应该去,如果有必要的话,使用字符串流来模拟从文件或标准输入中读取。
  • @UlrichEckhardt 我碰巧认为这是一个非常平衡的示例代码大小。 (如果你剥离输入,你只是让它成为非独立的。)
  • 请查一下什么是字符串流。使用它,您可以进一步减少代码。如果输入操作不是必需的并且可以用硬编码值替换,则可能更多。
  • 感谢您的提示。 sehe提供的链接实际上是我需要为这个项目做的。我仍然很难弄清楚如何编写算法

标签: c++ combinations permutation


【解决方案1】:

你能澄清一下最初的任务吗?从您的描述中,我看到您的首要任务是找到最小的数字集。是你想要的吗? 我只是假设您想尽快找到答案:) 那么你可以做什么:

  1. 对数组进行排序(降序)
  2. 启动递归函数(如果您愿意,可以将其重写为循环形式)

函数可能如下所示:

bool FindPriceToMatchFromPos(int pos, int PriceToMatch)
{
    if (ItemPrice[pos] == PriceToMatch) // Found the answer
    {
        resultIndices.push_back(pos); 
        return true;
    }
    else if (ItemPrice[pos] < PriceToMatch && pos < ItemPrice.size() - 1)
    {
        int residue = PriceToMatch - ItemPrice[pos];
        for (int i = pos + 1; i < ItemPrice.size(); i++)
        {
            if (FindPriceToMatchFromPos(i, residue))
            {
                resultIndices.push_back(pos);
                return true;
            }
        }
    }
    return false;
}

而你主要:

int main ()
{
// There will be your result indices (or you can store values instead)
vector<int> resultIndices; 

bool SolutionFound = false;
for (int CurrentPosition = 0; !SolutionFound && CurrentPosition < ItemPrice.size(); CurrentPosition++)
     SolutionFound = FindPriceToMatchFromPos(CurrentPosition,  PriceToMatch);
// Your results (if any) stored in "resultIndices"
}

提示:如果您的程序仅以 2 位小数精度运行,我建议将您的值乘以 100 并将它们存储为整数。这样你就不需要那个丑陋的比较功能了;) PS。对不起我的英语

有关您的问题的更多理论细节,您可以访问:Sum-subset with a fixed subset size

【讨论】:

  • 如果您想向作者提问,请使用 cmets,不要在答案中提问。
  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
  • @Diemuzi,我没有足够的声誉这样做
  • 非常感谢亚历克西!这实际上解决了我的问题!编辑:我没有足够的声誉来支持你的答案:(
猜你喜欢
  • 2017-07-02
  • 1970-01-01
  • 2022-08-18
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 2013-12-01
相关资源
最近更新 更多