I am relatively new to Java and to coding as well and am currently trying to solve a subset problem.
The goal is to have the user input the amount of numbers and then input every number up to the amount specified, with the computer asking for every n-th number accordingly.
Afterwards the computer should calculate the sum of all subsets, and return the one closest to pi, as well as the subset in this form [x,y,z] within the same line.
I managed the first part just fine, although it might have been improved with a switch-case for convenience. It adds the input numbers into the array
But I struggle with the second part of this problem, and I have no idea how to progress/arrange the code so that it outputs the desired result. The suggestion I got was that, a for-loop for a set with n elements :
for a from 0 to 2^n
for i from 0 to n
when the binary representation of x on has a 1 at the i-th position
add data[i] to solution.
This should supposedly find all subsets of an array. After that I should add each element, check if the distance from pi decreases and add the element to the solution set. Or at least that is the goal, but my code is not functional as I don't know where to start arranging it. I also don't know what to initialize bestsum with, or how the binary representation algorithm works, or how to add elements to the solution array in order. Edit : I have made progress, the code below outputs all the subarrays, and it properly calculates the closest sum, but I have no idea how to 'save' the best subarray (subarray with the closest sum) so that I can output it at the end with the sum. I am quite new so I haven't learned about lists or even methods, but this problem in this book at this chapter and I would like to solve it with the suggested method and possibly only simple loops.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers should be read? ");
int count = input.nextInt();
double[] data = new double[count];
double [] solution = new double[count];
double bestsum = 0.0;
for (int i = 0; i < count; i ) {
if ((i 1) % 10 == 1) {
System.out.println("Enter " (i 1) "st number: ");
data[i] = input.nextDouble();
} else if ((i 1) % 10 == 2) {
System.out.println("Enter " (i 1) "nd number: ");
data[i] = input.nextDouble();
} else if ((i 1) % 10 == 3) {
System.out.println("Enter " (i 1) "rd number: ");
data[i] = input.nextDouble();
} else {
System.out.println("Enter " (i 1) "th number: ");
data[i] = input.nextDouble();
}
}
for (int j = 0; j <(1 << count); j ) {
System.out.print("{ ");
double sum = 0.0;
for (int x = 0; x < count; x ) {
if ((j & (1 << x)) > 0) {
System.out.print(data[x] " ");
sum = sum data[x];
}
solution[x] = data[x];
}
System.out.println("}");
if (Math.abs(sum - Math.PI) < Math.abs(bestsum - Math.PI)) {
bestsum = sum;
}
}
System.out.println(bestsum);
System.out.println(Arrays.toString(solution));
}
}
CodePudding user response:
Here's my solution to this problem.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers should be read? ");
int count = input.nextInt();
Double data[] = new Double[count];
Double solution[] = new Double[count];
Double bestsum = 0.0;
List<Double> bestSet= Collections.emptyList();
Set<List<Double>> powerSet = new LinkedHashSet<>();
for (int i = 0; i < count; i ) {
if (i == 0) {
System.out.println("Enter " (i 1) "st number: ");
data[i] = input.nextDouble();
} else if (i == 1) {
System.out.println("Enter " (i 1) "nd number: ");
data[i] = input.nextDouble();
} else if (i == 2) {
System.out.println("Enter " (i 1) "rd number: ");
data[i] = input.nextDouble();
} else {
System.out.println("Enter " (i 1) "th number: ");
data[i] = input.nextDouble();
}
}
for (int i = 0; i < (1<<count); i )
{
int m = 1; // m is used to check set bit in binary representation.
List<Double> currentSet = new ArrayList<>();
for (int j = 0; j < count; j )
{
if ((i & m) > 0)
{
currentSet.add(data[j]);
}
m = m << 1;
}
powerSet.add(currentSet);
}
Iterator<List<Double>> iterator = powerSet.iterator();
iterator.next();
for (int i = 1; i < powerSet.size(); i ) {
Double sum=0.0;
List<Double> currentSet = iterator.next();
sum = currentSet.stream().collect(Collectors.summingDouble(Double::doubleValue));
if(Math.abs(sum-Math.PI)<Math.abs(bestsum-Math.PI)) {
bestSet = currentSet;
bestsum = sum;
}
}
System.out.println("powerSet: " powerSet);
System.out.println("solution: " bestSet);
System.out.println("bestsum: " bestsum);
}