基于 2 个变量的组合

发布于 2024-10-06 05:07:16 字数 337 浏览 2 评论 0原文

我想要完成的是为以下内容创建一个函数:

想象一下我有 1-9 个正方形。这些方块有一个全局编号,而不是单独编号。它们就像一个集合,而那个集合有这个数字。

例如:| _ | _ | _ | 19

我想做的是一个函数,它根据方块的数量和与它们相关的数字给出可能的组合。对于上面的示例:9、8、2 是可能的组合之一。然而,我只想要这些组合中的数字,而不是组合本身。另外,它们必须是唯一的(不应该是 9, 9, 1)。哦,这些数字范围为 1-9。

我怎样才能在C中实现这一点?如果您想知道这是一款益智游戏。

提前致谢!

What I'm trying to accomplish is making a function to the following:

Imagine that I have between 1-9 squares. Those squares have a number assigned to them globally, not individually. They are like a set, and that set has this number.

E.g.: | _ | _ | _ | 19

What I'm trying to do is a function that gives me the possible combinations depending on number of squares and the number associated with them. For the example above: 9, 8, 2 is one of the possible combinations. However I just want the numbers that are in those combinations, not the combinations themselves. Plus they have to be unique (shouldn't be 9, 9, 1). Oh and those numbers range from 1-9 only.

How can I achieve this in C? If you are wondering this is for a puzzle game.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我也只是我 2024-10-13 05:07:16

看起来您正在尝试查找右侧整数的受限分区。该链接应该为您提供一个良好的起点,并且您应该能够找到一些将整数分区生成任意数量部分的算法。

Looks like you are trying to find a restricted Partition of the integer on the right. The link should give you a good starting place, and you should be able to find some algorithms that generate partitions of an integer into an arbitrary number of parts.

梦萦几度 2024-10-13 05:07:16

为了将来的参考,在组合学中,我们说“顺序无关紧要”,意思是“我只想要一组数字,而不是特定的顺序”

//Sets the given digit array to contain the "first" set of numbers which sum to sum
void firstCombination(int digits[], int numDigits, int sum) { 
    reset(digits, 0, 1, numDigits, sum);
}

//Modifies the digit array to contain the "next" set of numbers with the same sum.
//Returns false when no more combinations left
int nextCombination(int digits[], int numDigits) {
    int i;
    int foundDiffering = 0;
    int remaining = 0;
    for (i = numDigits - 1; i > 0; i--) {
        remaining += digits[i];
        if (digits[i] - digits[i - 1] > 1) {
            if (foundDiffering || digits[i] - digits[i - 1] > 2) {
                digits[i - 1]++;
                remaining--;
                break;
            } else
                foundDiffering = 1;
        }
    }
    if (i == 0)
        return 0;
    else {
        reset(digits, i, digits[i - 1] + 1, numDigits - i, remaining);
        return 1;
    }
}

//Helper method for firstCombination and nextCombination
void reset(int digits[], int off, int lowestValue, int numDigits, int sum) {
    int i;
    int remaining = sum;
    for (i = 0; i < numDigits; i++) {
        digits[i + off] = lowestValue;
        remaining -= lowestValue;
        lowestValue++;
    }
    int currentDigit = 9;
    for (i = numDigits + off - 1; i >= off; i--) {
        if (remaining >= currentDigit - digits[i]) {
            remaining -= currentDigit - digits[i];
            digits[i] = currentDigit;
            currentDigit--;
        } else {
            digits[i] += remaining;
            break;
        }
    }
}

For future reference, in combinatorics we say "order doesn't matter" to mean "I only want the set of numbers, not a specific ordering"

//Sets the given digit array to contain the "first" set of numbers which sum to sum
void firstCombination(int digits[], int numDigits, int sum) { 
    reset(digits, 0, 1, numDigits, sum);
}

//Modifies the digit array to contain the "next" set of numbers with the same sum.
//Returns false when no more combinations left
int nextCombination(int digits[], int numDigits) {
    int i;
    int foundDiffering = 0;
    int remaining = 0;
    for (i = numDigits - 1; i > 0; i--) {
        remaining += digits[i];
        if (digits[i] - digits[i - 1] > 1) {
            if (foundDiffering || digits[i] - digits[i - 1] > 2) {
                digits[i - 1]++;
                remaining--;
                break;
            } else
                foundDiffering = 1;
        }
    }
    if (i == 0)
        return 0;
    else {
        reset(digits, i, digits[i - 1] + 1, numDigits - i, remaining);
        return 1;
    }
}

//Helper method for firstCombination and nextCombination
void reset(int digits[], int off, int lowestValue, int numDigits, int sum) {
    int i;
    int remaining = sum;
    for (i = 0; i < numDigits; i++) {
        digits[i + off] = lowestValue;
        remaining -= lowestValue;
        lowestValue++;
    }
    int currentDigit = 9;
    for (i = numDigits + off - 1; i >= off; i--) {
        if (remaining >= currentDigit - digits[i]) {
            remaining -= currentDigit - digits[i];
            digits[i] = currentDigit;
            currentDigit--;
        } else {
            digits[i] += remaining;
            break;
        }
    }
}
禾厶谷欠 2024-10-13 05:07:16

听起来你正在做的事情与kakuro非常相似,也称为交叉求和:http:// en.wikipedia.org/wiki/Cross_Sums

有一些用于此类谜题的生成器,例如: http://www.perlmonks.org/?node_id=550884

我怀疑大多数 kakuro 生成器都必须解决您的确切问题,因此您可能会查看一些以获取灵感。

It sounds like what you're working on is very similar to kakuro, also know as Cross Sums: http://en.wikipedia.org/wiki/Cross_Sums

There are generators out there for these kinds of puzzles, for example: http://www.perlmonks.org/?node_id=550884

I suspect that most kakuro generators would have to solve your exact problem, so you might look at some for inspiration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文