用 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
为了解决这个问题,我们将使用递归。并且为剩余值 n-s(其中 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP