在C ++中打印可以组成给定数字的所有点组合
在这个问题上,我们得到总分n。打印总得分为n的篮球得分的1、2和3的所有组合。
让我们看一个例子来了解这个问题,
Input: 4 Output: 1 1 1 1 1 1 2 1 2 1 1 3 2 1 1 2 2 3 1
为了解决这个问题,我们将使用递归。固定值和资源用于休息值ns,其中s是得分。如果组合加起来为n,则打印组合。
示例
该代码显示了我们代码的实现-
#define MAX_POINT 3
#define ARR_SIZE 100
#include <bits/stdc++.h>
using namespace std;
void printScore(int arr[], int arr_size) {
int i;
for (i = 0; i < arr_size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void printScoreCombination(int n, int i) {
static int arr[ARR_SIZE];
if (n == 0) {
printScore(arr, i);
}
else if(n > 0) {
int k;
for (k = 1; k <= MAX_POINT; k++){
arr[i]= k;
printScoreCombination(n-k, i+1);
}
}
}
int main() {
int n = 4;
cout<<"Different compositions formed by 1, 2 and 3 of "<<n<<" are\n";
printScoreCombination(n, 0);
return 0;
}输出结果
Different compositions formed by 1, 2 and 3 of 4 are 1 1 1 1 1 1 2 1 2 1 1 3 2 1 1 2 2 3 1
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便