ในปัญหานี้ เราได้คะแนนรวม 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