如何将两组花车与另外两组花车进行比较?

How to compare two sets of floats to another two sets of floats?

我目前正在创建一个程序来比较钻石的净度和重量。我将输入 n 颗钻石,并找到最长的钻石子序列,每次钻石的重量和净度都会变好。我纠结的是如何比较第一颗钻石的重量和第二颗钻石的重量等等,以及比较第一颗钻石的净度和第二颗钻石的净度等等。我不确定每次循环时是否只能进行 2 次比较,或者是否必须比较每颗钻石。任何帮助都会很棒。

第一个输入是测试用例的数量,下一个输入是测试用例中钻石的数量,接下来是 2 个由空格分隔的浮点数,分别代表重量和净度。

这是我的代码:

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();
            }

        }
        
    }

}

首先,您必须定义钻石何时比其他钻石“更好”,理想情况下,定义 partial order.

因为你的问题不需要(阅读时可以比较)但最好定义一个 Diamond class:

class Diamond {
    float weight;
    float clarity;
}

你可以在某处定义比较器,但通常在 class 本身

static class Diamond implements Comparable<Diamond> {
    float weight;
    float clarity;

    @Override
    public int compareTo(Diamond d) {
        return When `this` is better than d?;
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
}

现在您可以比较 Diamonds

Diamond c = a.compareTo(b) < 0 ? a: b;

如果你想一颗一颗地读钻石并且只得到最好的,你可以定义最差的Diamond(在Diamond class)

static final Diamond WORST = new Diamond(0.0f, 0.0f);

现在,从 stdin 读取您初始化最好的钻石,并在读取新钻石时变得更好

Diamond best = Diamond.WORST;
...
while(reading) {
    ...
    best = best.compareTo(next) < 0 ? next: best;
    ...
}

但是请记住,什么时候钻石 A 比 B 好?

旁白:有序集允许其元素之间的任何可能顺序,但通常对于数字,应该定义一个函数。体重越大越好吗?是不是越清晰越好?一种可能(但主观)的方法是按价格进行比较,然后您只需使用一些方程式或使用历史或市场数据

Diamond 转换为价格

现在,您可以在 Diamondclass

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

你的比较器将变成

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

一些不错的答案,但可能超出了 OP 的水平。下面是一个更基本的示例。

想想你需要做什么。您需要阅读 n 颗钻石的信息。此信息包括它们的重量和清晰度。这部分非常简单。

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();
        }
    }
}

但是,我们如何跟踪之前的重量和清晰度?好吧,如果是第一次通过就不用担心了!

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) {

            }
        }
    }
}

我们需要一个地方来存储在循环迭代之间保留的先前信息。哦,我们可能需要在循环迭代之间保持当前的权重和清晰度。完成此操作后,如果不是第一次通过,则很容易将当前值移动到以前的值。

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();
        }
    }
}

现在我们可以比较它们了。我们需要一个地方来存放我们“连续”获得的更好的钻石。 int 就可以了。如果当前钻石更好,我们可以增加它,否则将其重置为零。

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;
            }
        }
    }
}

但这只会让我们跟踪当前的连续记录,而不是最长的记录。为此,我们需要逻辑来比较当前的连胜长度与现有的最大值,并在达到新的最大值时修改最大值。

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;
            }
        }
    }
}