列出加起来等于y的x个数(必须大于0)的所有组合
C++ List all combinations of x amount of numbers (must be bigger than 0) that adds up to y?
我们如何列出所有x数量的数字加起来等于y的组合?这些数字必须大于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。
编辑:对于OPs问题不清楚的人。他想找出有多少种方法可以把某个正整数n写成k个正整数的和,其中k是固定的。
OP你需要自己做一些阅读和研究,这样你就可以给我们提供一些你自己的想法。我建议你仔细阅读……
分区http://en.wikipedia.org/wiki/Partition_ (number_theory)
和http://en.wikipedia.org/wiki/Memoization
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 大于65535的C++数组[size]引发不一致的溢出
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 查找最接近的大于当前数字的数字的索引
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 模板元编程:如何将参数包组合成新的参数包
- 检查向量是否具有所有可能的字符组合
- 如何在加密++中将两个源组合成新的源
- 根中的组合
- 如何找到大于整数的最小数字
- 为什么签名字符可以保存大于 127 的值?
- 更改 C++ 中的组合分类变量
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 组合字符串不适用于 libCurl,C++
- 列出加起来等于y的x个数(必须大于0)的所有组合