इस समस्या में, हमें फॉर्म के लिए n चर का एक रैखिक समीकरण दिया गया है,
coeff1(var1) + coeff2(var2) + … + coeffn(varn) = value
n चर वाले रैखिक समीकरण के हलों की संख्या ज्ञात कीजिए।
समस्या को समझने के लिए एक उदाहरण लेते हैं,
इनपुट
coeff[] = {3, 1}, value = 4 आउटपुट
1
स्पष्टीकरण
Equation : 3x + y = 4. Solution, x = 0, y = 4.
समाधान दृष्टिकोण
समस्या का एक सरल समाधान समीकरण के मूल्य का मूल्यांकन करना है। फिर इसे पुनरावर्ती रूप से कॉल करके मानों को अपडेट करें। यदि मान 0 है, तो समाधान गणना 1 है। अन्यथा गुणांक मानों को घटाकर मान के साथ पुनरावृत्ति करें।
हमारे समाधान की कार्यप्रणाली को दर्शाने वाला कार्यक्रम,
उदाहरण
#include<iostream>
using namespace std;
int countSolutionsEq(int coeff[], int start, int end, int value) {
if (value == 0)
return 1;
int coefCount = 0;
for (int i = start; i <= end; i++)
if (coeff[i] <= value)
coefCount += countSolutionsEq(coeff, i, end, value -
coeff[i]);
return coefCount;
}
int main() {
int coeff[] = {3, 5, 1, 2};
int value = 6;
int n = sizeof(coeff) / sizeof(coeff[0]);
cout<<"The number of solutions of the linear equation is "<<countSolutionsEq(coeff, 0, n - 1, value);
return 0;
} आउटपुट
The number of solutions of the linear equation is 8