簡體   English   中英

如何將兩組花車與另外兩組花車進行比較?

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

        }
        
    }

}

首先,您必須定義鑽石何時比其他鑽石“更好”,理想情況下,定義偏序

對於您的問題不需要(您可以在閱讀時進行比較)但最好定義一個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?;
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
}

現在你可以比較Diamond s

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

如果您想一顆一顆地閱讀鑽石並只獲得最好的,您可以定義最差的Diamond (在Diamond類上)

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?

另外:有序集允許其元素之間的任何可能順序,但通常對於數字,應定義 function。 體重越大越好嗎? 是不是越清晰越好? 一種可能(但主觀)的方法是按價格進行比較,然后您只需使用一些方程式或使用歷史或市場數據將您的Diamond轉換為價格

在此處輸入圖像描述

現在,您可以在Diamond s class 中定義價格(或不是實際價格的“相對價格”

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM