C++ 程序,用于使用最多为 K 的自然数求和到 N 并允许重复
给定两个整数N和K ,任务是找到将N表示为[1, K]范围内的正整数之和的总方法数,其中每个整数可以选择多次。
例子:
Input: N = 8, K = 2
Output: 5
Explanation: All possible ways of representing N as sum of positive integers less than or equal to K are:
- {1, 1, 1, 1, 1, 1, 1, 1}, the sum is 8.
- {2, 1, 1, 1, 1, 1, 1}, the sum is 8.
- {2, 2, 1, 1, 1, 1}, the sum is 8.
- 2, 2, 2, 1, 1}, the sum is 8.
- {2, 2, 2, 2}}, the sum is 8.
Therefore, the total number of ways is 5.
Input: N = 2, K = 2
Output: 2
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。