इस समस्या में, हमें कुल स्कोर n दिया गया है। उन सभी बास्केटबॉल पॉइंट्स के संयोजन प्रिंट करें जो 1, 2, और 3 हैं और जिनका कुल स्कोर n है।
उदाहरण
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