You are given a collection of coins of various denominations (different monetary
ID: 3841373 • Letter: Y
Question
You are given a collection of coins of various denominations (different monetary values) and are asked to choose some or all of the coins to pay for an item of a given price without needing to be given change - ie the exact amount.
How can this be viewed as a search problem? [5 marks]
You are provided with a class Coin which has a single method getValue. If the problem starts from an array of coins, how would you represent the current state? Your answer should include a class definition showing the needed fields and method signatures. You should not provide method bodies. [10 marks]
Provide the Java code for a method getDaughters in your state representation that given a state, would generate the daughter
states. You will not be penalised for minor syntactic errors.
[10 marks]
iv. Provide the Java code for the execution of a depth first search over the states, using the class definition above. [10 marks]
Explanation / Answer
class ssort
public static int[] SORT(int[] arr)
{
for(int i =0;i<arr.length-1;i++)
{
int index=i;
for(int j=i+1;j<arr.length;j++)
if(arr[j]<arr[index])
index=j;
int sm =arr[index];
arr[index]=arr[i];
arr[i]=sm;
}
return arr;
}
public static void main(String a[])
{
int [] arr1={1,2,2,1,3,4,5,6}
int [] arr2=SORT(arr1);
for(int i:arr2)
{
System.out.print(i);
System.out.print(",");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.