简体   繁体   English

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

[英]How to compare two sets of floats to another two sets of floats?

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.我将输入 n 颗钻石,并找到最长的钻石子序列,每次钻石的重量和净度都会变好。 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.我不确定每次循环时是否只能进行 2 次比较,或者是否必须比较每颗钻石。 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.第一个输入是测试用例的数量,下一个输入是测试用例中钻石的数量,接下来是 2 个由空格分隔的浮点数,分别代表重量和净度。

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

        }
        
    }

}

First of all, you must define when a diamond is "better" than other, ideally, defining a partial order .首先,您必须定义钻石何时比其他钻石“更好”,理想情况下,定义偏序

For your problem is not needed (you could compare when read) but is better define a Diamond class:对于您的问题不需要(您可以在阅读时进行比较)但最好定义一个Diamond class:

class Diamond {
    float weight;
    float clarity;
}

you can define a comparator somewhere but usually in the class itself你可以在某处定义一个比较器,但通常在 class 本身

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

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

now you can compare Diamond s现在你可以比较Diamond s

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

if you want read diamond one by one and get only the best, you could define the worst Diamond (on Diamond class)如果您想一颗一颗地阅读钻石并只获得最好的,您可以定义最差的Diamond (在Diamond类上)

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

now, reading from stdin you initialize the best diamond and get better when read a new one现在,从stdin读取你初始化最好的钻石并在读取新钻石时变得更好

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

but remember, when the diamond A is better than B?但请记住,钻石 A 何时优于 B?

Aside: an ordered set admit any possible order between their elements but, usually for numbers, a function should be defined.另外:有序集允许其元素之间的任何可能顺序,但通常对于数字,应定义 function。 Is better more weight?体重越大越好吗? Is better more clarity?是不是越清晰越好? A possible (but subjective) way is compare by price then you simply transform your Diamond to a price using some equation or using historical or market data一种可能(但主观)的方法是按价格进行比较,然后您只需使用一些方程式或使用历史或市场数据将您的Diamond转换为价格

在此处输入图像描述

now, you could define the price (or "relative price" where it's not a real price) in your Diamond s class现在,您可以在Diamond s class 中定义价格(或不是实际价格的“相对价格”

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

and you comparator will become你的比较器将成为

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

Some good answers, but perhaps beyond the level of the OP.一些很好的答案,但可能超出了 OP 的水平。 A more basic example follows.下面是一个更基本的例子。

Think about what you need to do.想想你需要做什么。 You need to read in info for n diamonds.您需要阅读n颗钻石的信息。 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.一个int就可以了。 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;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM