8 Replies - 457 Views - Last Post: 06 October 2009 - 03:08 PM Rate Topic: -----

#1 Ricendithas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 12-July 08

Can somebody help me understand this code?

Posted 05 October 2009 - 06:14 AM

Hi! This is the code I am using:

import java.util.*;
import java.io.*;

public class CountingChange {
	
	public static int[] coin = {50, 25, 10, 5, 1};
	public static long[] nway = new long[10000];
	
	public static void main(String[] args) throws FileNotFoundException {
		Scanner iFile = new Scanner(System.in);
		int amount = iFile.nextInt();
		
		nway[0] = 1;
		for(int i = 0; i < coin.length; i++) {
			int c = coin[i];
			for(int j = c; j <= amount; j++)
				nway[j] += nway[j-c]; // THIS ONE. WHAT'S THE USE OF THIS?
		}
		
		for(int i = 0; i <= amount; i++)
			System.out.print(nway[i] + " ");
		
		System.out.println();
		System.out.println(nway[amount]);
	}
}



So, I was wondering, what does the one with the comment do? I mean, what's the purpose of having that?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Can somebody help me understand this code?

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Can somebody help me understand this code?

Posted 05 October 2009 - 06:17 AM

it adds the element at index j in array nway to the element at nway[j-c]; and assigns the result back to nway[j]
Was This Post Helpful? 0
  • +
  • -

#3 Ricendithas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 12-July 08

Re: Can somebody help me understand this code?

Posted 05 October 2009 - 06:23 AM

View Postmostyfriedman, on 5 Oct, 2009 - 05:17 AM, said:

it adds the element at index j in array nway to the element at nway[j-c]; and assigns the result back to nway[j]


What I mean is that what is the main purpose of having it there? And why must we add it to the element at nway[j-c]?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Can somebody help me understand this code?

Posted 05 October 2009 - 06:51 AM

sorry but i can't answer that since i don't know what the algorithm is supposed to be doing
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • ¿Dónde están mis pantalones?
  • member icon





Reputation: 1968
  • View blog
  • Posts: 14,472
  • Joined: 24-September 08

Re: Can somebody help me understand this code?

Posted 05 October 2009 - 07:44 AM

Yea, it subtracts the current element of the coin array from the current nway element. That code doesn't make a lot of since. Where is it from and what is it SUPPOSED to do?
Was This Post Helpful? 0
  • +
  • -

#6 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Can somebody help me understand this code?

Posted 05 October 2009 - 12:16 PM

My deductive skill tells me that it's an algorithm that count the number of ways you can create change for a certain amount.

suppose we know the number of ways nway[A] to make change for amout A with coins c1, c2,...,c{k-1}, and for all amounts less then A (with cons c1,..., ck). The number of ways to make change for A including coin ck is then equal to nway[A] + the number of ways to do it for amount A-ck (with ck included);

nway[A] += nway[A-ck]

Perhaps not entirely obvious that this works though, but it does!

This post has been edited by LaFayette: 05 October 2009 - 12:17 PM

Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Can somebody help me understand this code?

Posted 05 October 2009 - 04:37 PM

One thing for sure, without this statement the nway[] array will never be initialized :)
Looks like a very complicated way to figure out how many coins of each type should be given to provide the change for a certain amount using an iteration rather than a simple divide followed my a modulo to find the change still to be reimburse
Was This Post Helpful? 0
  • +
  • -

#8 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Can somebody help me understand this code?

Posted 06 October 2009 - 03:20 AM

Quote

Looks like a very complicated way to figure out how many coins of each type should be given to provide ..


It doesnt figure that out. It figures out how many combinations of the given coins there exists that adds up to the amount given.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Can somebody help me understand this code?

Posted 06 October 2009 - 03:08 PM

View PostLaFayette, on 6 Oct, 2009 - 02:20 AM, said:

Quote

Looks like a very complicated way to figure out how many coins of each type should be given to provide ..


It doesnt figure that out. It figures out how many combinations of the given coins there exists that adds up to the amount given.

That makes more sense
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1