Home > Blockchain >  How to compare two sets of floats to another two sets of floats?
How to compare two sets of floats to another two sets of floats?

Time:09-29

I am currently creating a program to compare clarity and weights of diamonds. I will take an input of n diamonds and find the longest subsequence of diamonds where their weight and clarity gets better each time. What I'm struggling with is how to compare the weight of the first diamond to the weight of the second diamond and so on, as well as comparing the clarity of the first diamond to the clarity of the second diamond and so on. I'm not sure if I can just do 2 comparisons each time I loop, or if I have to compare every single diamond. Any help would be great.

The first input is the number of test cases, the next input is the amount of diamonds in the test case and the following will be 2 floats separated by spaces representing the weight and clarity, respectively.

Here is my code:

import java.util.Scanner;

public class Diamonds {
    
    static int i;
    static int j;
    
    /**
     * If a diamond has high weight but low clarity, it will be worth less than a diamond with
     * low weight but high clarity, what we are trying to figure out is the best sequence of diamonds
     * where the value of the diamond goes up each time, we will need to compare the quality of the 
     * previous diamond in order to see if the next diamond is more valuable. In order to do this, 
     * The size needs to be bigger than the previous diamond and the clarity needs to be less than the
     * previous diamond. 
     */
    
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        // Get number of test cases
        int test = scan.nextInt();
        
        
        // Use for loop to loop through n times until all test cases are completed
        for (i = 0; i < test; i  ) {
            
            // Get number of diamonds in test case
            int diamonds = scan.nextInt();
            
            // Loop through the amount of diamonds n times until all diamonds have been compared
            for (j = 0; j < diamonds; j  ) {
                float weight = scan.nextFloat();
                float clarity = scan.nextFloat();
            }

        }
        
    }

}

CodePudding user response:

Some good answers, but perhaps beyond the level of the OP. A more basic example follows.

Think about what you need to do. You need to read in info for n diamonds. This info comprises their weight and clarity. This part is pretty straightforward.

import java.util.Scanner;

public class Diamonds {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int numOfIterations = scanner.nextInt();

        for (int i = 0; i < n; i  ) {
            float weight = scan.nextFloat();
            float clarity = scan.nextFloat();
        }
    }
}

But, how do we keep track of the previous weight and clarity? Well, no point in worry about that if it's the first time through!

import java.util.Scanner;

public class Diamonds {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int numOfIterations = scanner.nextInt();

        for (int i = 0; i < n; i  ) {
            float weight = scan.nextFloat();
            float clarity = scan.nextFloat();

            if (i > 0) {

            }
        }
    }
}

We'll need a place to store the previous info that sticks around between loop iterations. Oh, and we probably need the current weight and clarity to stick around between loop iterations. Once we've done this, it's easy to move the current values into the previous values if it's not the first time through.

import java.util.Scanner;

public class Diamonds {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int numOfIterations = scanner.nextInt();

        float prevWeight  = 0.0;
        float prevClarity = 0.0;
        float curWeight   = 0.0;
        float curClarity  = 0.0;

        for (int i = 0; i < n; i  ) {
            if (i > 0) {
                prevWeight  = curWeight;
                prevClarity = curClarity;
            }

            curWeight = scan.nextFloat();
            curClarity = scan.nextFloat();
        }
    }
}

Now we can compare them. We'll need a place to store our "streak" of better diamonds. An int will do. We can increment it if the current diamond is better, or reset it to zero if it's not.

import java.util.Scanner;

public class Diamonds {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int numOfIterations = scanner.nextInt();

        float prevWeight  = 0.0;
        float prevClarity = 0.0;
        float curWeight   = 0.0;
        float curClarity  = 0.0;

        int streak = 0;

        for (int i = 0; i < n; i  ) {
            if (i > 0) {
                prevWeight  = curWeight;
                prevClarity = curClarity;
            }

            curWeight = scan.nextFloat();
            curClarity = scan.nextFloat();

            if (curWeigt > prevWeight && curClarity > prevClarity) {
                streak  ;
            }
            else {
                streak = 0;
            }
        }
    }
}

But this will only let us track the current streak, not the longest one. For that, we need logic to compare the current streak length to the existing maximum, and to modify the maximum if a new max has been reached.

import java.util.Scanner;

public class Diamonds {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int numOfIterations = scanner.nextInt();

        float prevWeight  = 0.0;
        float prevClarity = 0.0;
        float curWeight   = 0.0;
        float curClarity  = 0.0;

        int curStreak = 0;
        int maxStreak = 0;

        for (int i = 0; i < n; i  ) {
            if (i > 0) {
                prevWeight  = curWeight;
                prevClarity = curClarity;
            }

            curWeight = scan.nextFloat();
            curClarity = scan.nextFloat();

            if (curWeigt > prevWeight && curClarity > prevClarity) {
                streak  ;
            }
            else {
                streak = 0;
            }

            if (curStreak > maxStreak) {
                maxStreak = curStreak;
            }
        }
    }
}

CodePudding user response:

First of all, you must define when a diamond is "better" than other, ideally, defining a enter image description here

now, you could define the price (or "relative price" where it's not a real price) in your Diamonds class

float relativePrice() {
    return ...table interpolation...;
}

and you comparator will become

int compareTo(Diamond d) {
    return Float.compare(relativePrice(), d.relativePrice());
}
  • Related