मैं अपने सिर को खरोंच कर रहा हूं और यह मुझे खा रहा है। मुझे पता है कि यह जटिल नहीं है। मेरे पास कई आइटम हैं, यह संख्या बराबर या तीन से अधिक हो सकती है। तो मुझे उन वस्तुओं के समूह के संभावित संयोजन को निर्धारित करने की आवश्यकता है जो कुल को पूरा करेंगे। केवल एक ही प्रतिबंध यह है कि समूहों में तीन या अधिक वस्तुएं होनी चाहिए, जो सात वस्तुओं (लेकिन सहित) से अधिक नहीं होनी चाहिए।आइटम के संभावित समूहों को निर्धारित करने के लिए एल्गोरिदम
उदाहरण के लिए:
अगर मैं 7 आइटम नहीं हैं, तो मैं इन संभव समूह बना सकते हैं:
- 1 7 वस्तुओं के समूह।
- 4 आइटमों का 1 समूह और 3 आइटमों का 1 समूह।
अगर मैं 12 आइटम नहीं हैं, मैं इन संभव समूह बना सकते हैं:
- 4 3 आइटम के समूहों।
- 4 आइटम के 3 समूह।
- 6 आइटम के 2 समूह।
- 7 आइटमों का 1 समूह + 5 आइटमों का 1 समूह।
- 6 आइटमों के 3 और 1 समूह के 2 समूह।
- 3 का 1 समूह, 4 समूह के 1 समूह और पांच वस्तुओं के 1 समूह।
- ...
मैं प्रत्यावर्तन के बारे में सोचा और कलन विधि को लागू करने शुरू कर दिया। यह स्पष्ट रूप से काम नहीं कर रहा है। मैं रिकर्सन पर चूसना। बहुत।
//Instance Fields
public List<ArrayList<String>> options;
//Method that will generate the options. The different options are
//stored in a list of "option". An individual option will store a list of
//strings with the individual groups.
public void generateOptions(int items, ArrayList<String> currentOption){
//If the current option is null, then create a new option.
if(currentOption == null){
currentOption = new ArrayList<String>();
}
if(items < 3){
//If the number of items is less than three then it doesn't comply with the
//requirements (teams should be more or equal than three.
currentOption.add("1 group of "+items+" items");
options.add(currentOption);
}
else{
//I can make groups of 3,4,5,6 and 7 items.
for(int i = 3;i<=7;i++){
if(items%i == 0){
// If the number of items is divisible per the current number,
// then a possible option could be items/i groups of i items.
// Example: Items = 9. A possible option is 3 groups of 3 items.
currentOption.add(items/i +" groups of "+ i+" items");
options.add(currentOption);
}
else{
// If the number of items - the current number is equal or greater than
// three, then a possible option could be a group of i items
// and then I'll have items-i items to separate in other groups.
if(items - i >=3){
currentOption.add("1 group of "+i+" items");
generateOptions(items-i,currentOption);
}
}
}
}
}
आपकी मदद के लिए धन्यवाद !!!
यह जो मैं देख रहा हूं उससे बहुत करीब है। मैं यह देखने के लिए जावा में इसे बदलने की कोशिश कर रहा हूं कि यह कैसे काम करता है। धन्यवाद। – miguelrios