-1

我们如何列出加起来为 y 的 x 个数的所有组合?这些数字必须大于 0。

注意:不接受 0。我们只能添加 x 个加起来等于 y 的正数。

#include <string>
#include <iostream>

// Prototype.
bool Function( unsigned int in_x, unsigned int in_y );

int main() {
    // Declarations.
    unsigned int x = 0;
    unsigned int y = 0;

    // Ask for x.
    std::cout << "\nEnter x:\n";
    std::cin >> x;

    // Ask for y.
    std::cout << "\nEnter y:\n";
    std::cin >> y;

    // Begin.
    Function( x, y );

    // Notify and stop.
    std::cout << "\nDone!\n";
    std::cin.ignore();
    ::getchar();

    // Exit with success.
    return 0;
};

bool Function( unsigned int in_x, unsigned int in_y ) {
    // Error handler.
    if( in_x == 0 ) {
        return false; }

    // Generate and list...
    // *Note: 0 is not accepted. We can only add x amount of positive numbers that adds up to y.
    // How do we do it?

    return false;
};

输入和输出示例:

示例 #1:

Enter x:
3

Enter y:
5

//          x               |   y
0>      3   +   1   +   1   =   5
1>      2   +   2   +   1   =   5
Count = 2

Done!

示例 #2:

Enter x:
2

Enter y:
5

//          x       |   y
0>      3   +   2   =   5
1>      4   +   1   =   5
Count = 2

Done!

x 是 2。这意味着,我们的函数只能生成两个加起来等于 y 的正数,即 5。

4

1 回答 1

1

编辑:对于任何不清楚 OPs 问题的人。他想找出将某个正整数 n 写为 k 个正整数之和的方法的数量,其中 k 是固定的。

OP,您需要在这里进行一些自己的阅读和研究,以便您可以向我们提供您自己的一些想法。我建议你阅读...

分区 http://en.wikipedia.org/wiki/Partition_(number_theory)

http://en.wikipedia.org/wiki/Memoization

于 2013-03-31T18:17:15.227 回答