473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

combination code output needs explanation

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2.     import java.util.List;
  3. public class CombinationApp {
  4.  
  5.         private static int[] numbers= {1,2,3,4,5,6,7,8,9,10};
  6.         private static int[] sumsum= new int[numbers.length];
  7.  
  8.         private static int sum= 10;
  9.  
  10.         private static void solution(List<Integer> solution) {
  11.  
  12.             System.out.println(solution);
  13.         }
  14.  
  15.         private static void solve(int[] numbers, int i, int sum, List<Integer> solution) {
  16.  
  17.             if (sum == 0) 
  18.                 solution(solution);
  19.             else
  20.                 for (; i < numbers.length && sumsum[i] >= sum; i++) {
  21.                     if (numbers[i] <= sum) {
  22.                         solution.add(0, numbers[i]);
  23.                         solve(numbers, i+1, sum-numbers[i], solution);
  24.                         solution.remove(0);
  25.                     }
  26.                 }
  27.         }
  28.  
  29.         public static void main(String[] args) {
  30.  
  31.             for (int s= 0, i= numbers.length; --i >= 0; ) {
  32.                 sumsum[i]= numbers[i]+s;
  33.                 s+= numbers[i];
  34.             }
  35.             solve(numbers, 0, sum, new ArrayList<Integer>());
  36.         }
  37.     }
  38.  
  39.  
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]
Apr 3 '13 #1
2 1316
Rabbit
12,516 Expert Mod 8TB
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.
Apr 3 '13 #2
r035198x
13,262 8TB
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
Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args) {
  2.         int[] numberlist = {1,2,3,4,5,6,7,8,9,10};
  3.         Set<String> answersList = new HashSet<String>();
  4.         findSums(numberlist, 0, 0, 10, "", answersList);
  5.         System.out.println(answersList);
  6.  
  7.     }
  8.  
  9.     public static void findSums(int[] list, int index, int current, int K, String previousList, Set<String> answers) {
  10.         if (list.length < index || current > K)
  11.             return;
  12.         for (int i = index; i < list.length; i++) {
  13.             if (current + list[i] == K) {
  14.                 answers.add(previousList + " " + list[i]);
  15.             } else if (current + list[i] < K) {
  16.                 findSums(list, i + 1, current + list[i], K, previousList + " " + list[i], answers);
  17.             }
  18.         }
  19.     }
  20.  
Apr 4 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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,...
3
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...
6
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() {
4
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...
14
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
7
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)
12
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. ...
2
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...
25
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
jinu1996
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.