-
import java.util.ArrayList;
-
import java.util.List;
-
public class CombinationApp {
-
-
private static int[] numbers= {1,2,3,4,5,6,7,8,9,10};
-
private static int[] sumsum= new int[numbers.length];
-
-
private static int sum= 10;
-
-
private static void solution(List<Integer> solution) {
-
-
System.out.println(solution);
-
}
-
-
private static void solve(int[] numbers, int i, int sum, List<Integer> solution) {
-
-
if (sum == 0)
-
solution(solution);
-
else
-
for (; i < numbers.length && sumsum[i] >= sum; i++) {
-
if (numbers[i] <= sum) {
-
solution.add(0, numbers[i]);
-
solve(numbers, i+1, sum-numbers[i], solution);
-
solution.remove(0);
-
}
-
}
-
}
-
-
public static void main(String[] args) {
-
-
for (int s= 0, i= numbers.length; --i >= 0; ) {
-
sumsum[i]= numbers[i]+s;
-
s+= numbers[i];
-
}
-
solve(numbers, 0, sum, new ArrayList<Integer>());
-
}
-
}
-
-
why is it that this is the output ?
[4, 3, 2, 1]
[7, 2, 1]
[6, 3, 1]
[5, 4, 1]
[9, 1]
[5, 3, 2]
[8, 2]
[7, 3]
[6, 4]
[10]
2 1316
Because that combination of values adds up to the target number, in this case 10. Since it's a recursive function, it repeatedly calls itself to add every combination of numbers in the array until it hits the target value or runs out of numbers to add.
It's a normal depth first algorithm so maybe revise that algorithm and have a look at the code again.
Here is a variation without the main method setup -
public static void main(String[] args) {
-
int[] numberlist = {1,2,3,4,5,6,7,8,9,10};
-
Set<String> answersList = new HashSet<String>();
-
findSums(numberlist, 0, 0, 10, "", answersList);
-
System.out.println(answersList);
-
-
}
-
-
public static void findSums(int[] list, int index, int current, int K, String previousList, Set<String> answers) {
-
if (list.length < index || current > K)
-
return;
-
for (int i = index; i < list.length; i++) {
-
if (current + list[i] == K) {
-
answers.add(previousList + " " + list[i]);
-
} else if (current + list[i] < K) {
-
findSums(list, i + 1, current + list[i], K, previousList + " " + list[i], answers);
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J |
last post by:
Hi,
Ive got 3 forms, (a subform within a subform within a form) and when I
refresh the outermost form, Access closes and offers to send an error
report.
The forms worked fine a few weeks ago,...
|
by: ljubo lecic via AccessMonster.com |
last post by:
I have written a pretty complex ACCESS accounting aplication which is
working fine.But under some circumstances I get this nasty error message:
"Microsoft ACCESS has encauntered a problem and needs...
|
by: Buck Rogers |
last post by:
Hi guys! Love your work!
The below program is from K&R2, p22.
=================================
#include <stdio.h>
/* count digits, white space, others */
main()
{
|
by: vib |
last post by:
Hi there,
I am looking at this code for quite awhile, still don't any clue why
one wants to do it this way. It is about display message through UART
or COM port. Here is the code.
The calling...
|
by: Akhil |
last post by:
plz c d following code
#include<stdio.h>
void main()
{
int x=4,y=1;
y=x++++; //gives error message lvalue required
y=x++ + ++y;//no errors
|
by: s99999999s2003 |
last post by:
hi
my friend has written a loop like this
cnt = 0
files =
while cnt < len(files) :
do_something(files)
i told him using
for fi in files:
do_something(fi)
|
by: garyusenet |
last post by:
I have had no replies to my previous post so perhaps I didn't write it
good enough.
Please excuse new thread but i wanted to break from the last thread
hopefully this thread will be better.
...
|
by: Milsnips |
last post by:
Hi there,
i have a test application with 2 forms - frmMain and frmDialog
the application starts with frmMain (which has a label on it called
lblStatus), and when i click a button, it opens the...
|
by: Erik Lind |
last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more
online and can write simple modules, but I still don't get when __init__
needs to be used as opposed to creating a class...
|
by: ejaggers |
last post by:
I'm teaching myself OOP by following examples. I can't figure out what this is doing for the life of me. I've read the explanation and still don't get it. Can anyone explain, plus is there a more...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |