0

这是问题陈述

一个印度农民有一块农田,比如说 L 平方公里长,想要播种小麦或水稻或两者兼而有之。农民有有限数量的 F 公斤化肥和 P 公斤杀虫剂。

每平方公里小麦需要 F1 公斤的肥料和 P1 公斤的杀虫剂。每平方公里水稻种植需要 F2 Kg 肥料和 P2 Kg 杀虫剂。令 S1 为出售一平方公里收获的小麦所获得的价格,而 S2 为出售一平方公里收获的大米所获得的价格。

您必须通过选择应该种植小麦和/或稻米的区域来找到农民可以获得的最大总利润。

例如:

L = 10 Km2,F = 10 Kg,P = 5 Kg,F1 = 2 Kg,P1 = 2 Kg,F2 = 3 Kg,P2 = 1 Kg,S1 = 14,S2 = 25。

在这种情况下,如果农民在 Km2 面积上只播种水稻,则将获得最大利润3.33,最大利润值为83.33

输入格式

唯一的输入将由 9 个以空格分隔的整数组成,L, F, P, F1, P1, F2, P2, S1, S2

约束

1 <= L <= 10^4
1 <= F <= 10^4
1 <= P <= 10^4
F1 + F2< = F
P1 + P2 <= P
1 <= S1 <= 10^4
1 <= S2 <= 10^4

输出格式

输出将是

  • 最大利润修正高达 2 位数
  • 小麦应种植的公里数 2 位数
  • 以km 2 为单位的值,水稻应正确种植最多2 位数字。

对于问题输出中考虑的示例,将是83.33 0.00 3.33.

示例测试用例

输入

10 10 5 2 2 3 1 14 25

输出

83.33 0.00 3.33

解释

假设 L = 10 Km2,F = 10 Kg,P = 5 Kg,F1 = 2 Kg,P1 = 2 Kg,F2 = 3 Kg,P2 = 1 Kg,S1 = 14,S2 = 25。如果农民在 3.33 平方公里的土地上不种植小麦而种植水稻,则总利润最大,最大利润值为 83.33 。

在此处输入图像描述

我需要解决这个问题。但是,无法掌握语句本身。请帮我。

4

4 回答 4

1

这是一个线性优化问题(https://en.wikipedia.org/wiki/Linear_programming),通常由单纯形算法(https://en.wikipedia.org/wiki/Simplex_algorithm)解决。

于 2020-06-25T09:59:19.560 回答
1
import java.text.DecimalFormat;
import java.util.Scanner;

public class CandidateCode {

    public static void main(String args[]) {
        // String input = "10,10,5,2,2,3,1,14,25";
        System.out.print(get_total_profit());
    }

    public static String get_total_profit() {
        // String[] inputs = input1.split(",");
        // Piece of farm land in square kilometer
        Scanner in = new Scanner(System.in);
        float L = in.nextInt(); // Float.valueOf(inputs[0]);
        // Fertilizer in kg
        float F = in.nextInt();// Float.valueOf(inputs[1]);
        // Insecticide in kg
        float P = in.nextInt();// Float.valueOf(inputs[2]);
        // Fertilizer required in kg for square kilometer of Wheat
        float F1 = in.nextInt();// Float.valueOf(inputs[3]);
        // Insecticide required in kg for square kilometer of Wheat
        float P1 = in.nextInt();// Float.valueOf(inputs[4]);
        // Fertilizer required in kg for square kilometer of Rice
        float F2 = in.nextInt();// Float.valueOf(inputs[5]);
        // Insecticide required in kg for square kilometer of Rice
        float P2 = in.nextInt();// Float.valueOf(inputs[6]);
        // Selling price of wheat per square kilometer
        float S1 = in.nextInt();// Float.valueOf(inputs[7]);
        // Selling price of rice per square kilometer
        float S2 = in.nextInt();// Float.valueOf(inputs[8]);

        // Result Variables
        float totalRiceInsecUsed = 0f;
        float totalRiceFertUsed = 0f;
        float totalWheatInsecUsed = 0f;
        float totalWheatFertUsed = 0f;
        float areaOfWheat = 0.00f;
        float areaOfRice = 0.00f;
        float amount = 0.00f;

        while (true) {
            if ((L == areaOfRice + areaOfWheat) || P == totalRiceInsecUsed + totalWheatInsecUsed
                    || F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0 || F1 == 0 || P2 == 0 || P1 == 0) {
                break;
            }

            float calRiceProfit = Math.min(F / F2, P / P2) * S2;
            float calWheatProfit = Math.min(F / F1, P / P1) * S1;

            if (calRiceProfit > calWheatProfit) {
                float areaInsecUsed = P / P2;
                float areaFertUsed = F / F2;
                if (areaInsecUsed > areaFertUsed) {
                    L = L - areaFertUsed;
                    F2 = 0;
                    totalRiceFertUsed = totalRiceFertUsed + F2;
                    areaOfRice = areaOfRice + areaFertUsed;
                    amount = amount + areaFertUsed * S2;
                } else if (areaInsecUsed < areaFertUsed) {
                    L = L - areaInsecUsed;
                    P2 = 0;
                    totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
                    areaOfRice = areaOfRice + areaInsecUsed;
                    amount = amount + areaInsecUsed * S2;
                }
            } else {
                float areaInsecUsed = P / P1;
                float areaFertUsed = F / F1;
                if (areaInsecUsed > areaFertUsed) {
                    L = L - areaFertUsed;
                    F1 = 0;
                    totalWheatFertUsed = totalWheatFertUsed + F1;
                    areaOfWheat = areaOfWheat + areaFertUsed;
                    amount = amount + areaFertUsed * S1;
                } else if (areaInsecUsed < areaFertUsed) {
                    L = L - areaInsecUsed;
                    P1 = 0;
                    totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
                    areaOfWheat = areaOfWheat + areaInsecUsed;
                    amount = amount + areaInsecUsed * S1;
                }
            }

        }
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        return df.format(amount) + " " + df.format(areaOfWheat) + " " + df.format(areaOfRice);
    }

}
于 2020-07-11T06:32:28.770 回答
0
import java.text.DecimalFormat;
import java.util.Scanner;

public class Test {

    public static void main(String args[]) {
        // String input = "10,10,5,2,2,3,1,14,25";
        System.out.println(get_total_profit());
    }

    public static String get_total_profit() {
        // String[] inputs = input1.split(",");
        // Piece of farm land in square kilometer
        Scanner in = new Scanner(System.in);
        float L = in.nextInt(); // Float.valueOf(inputs[0]);
        // Fertilizer in kg
        float F = in.nextInt();// Float.valueOf(inputs[1]);
        // Insecticide in kg
        float P = in.nextInt();// Float.valueOf(inputs[2]);
        // Fertilizer required in kg for square kilometer of Wheat
        float F1 = in.nextInt();// Float.valueOf(inputs[3]);
        // Insecticide required in kg for square kilometer of Wheat
        float P1 = in.nextInt();// Float.valueOf(inputs[4]);
        // Fertilizer required in kg for square kilometer of Rice
        float F2 = in.nextInt();// Float.valueOf(inputs[5]);
        // Insecticide required in kg for square kilometer of Rice
        float P2 = in.nextInt();// Float.valueOf(inputs[6]);
        // Selling price of wheat per square kilometer
        float S1 = in.nextInt();// Float.valueOf(inputs[7]);
        // Selling price of rice per square kilometer
        float S2 = in.nextInt();// Float.valueOf(inputs[8]);

        // Result Variables
        float totalRiceInsecUsed = 0f;
        float totalRiceFertUsed = 0f;
        float totalWheatInsecUsed = 0f;
        float totalWheatFertUsed = 0f;
        float areaOfWheat = 0.00f;
        float areaOfRice = 0.00f;
        float amount = 0.00f;

        while (true) {
            if ((L == areaOfRice + areaOfWheat) || P == totalRiceInsecUsed + totalWheatInsecUsed
                    || F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0 || F1 == 0 || P2 == 0 || P1 == 0) {
                break;
            }

            float calRiceProfit = Math.min(F / F2, P / P2) * S2;
            float calWheatProfit = Math.min(F / F1, P / P1) * S1;

            if (calRiceProfit > calWheatProfit) {
                float areaInsecUsed = P / P2;
                float areaFertUsed = F / F2;
                if (areaInsecUsed > areaFertUsed) {
                    L = L - areaFertUsed;
                    F2 = 0;
                    totalRiceFertUsed = totalRiceFertUsed + F2;
                    areaOfRice = areaOfRice + areaFertUsed;
                    amount = amount + areaFertUsed * S2;
                } else if (areaInsecUsed < areaFertUsed) {
                    L = L - areaInsecUsed;
                    P2 = 0;
                    totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
                    areaOfRice = areaOfRice + areaInsecUsed;
                    amount = amount + areaInsecUsed * S2;
                }
            } else {
                float areaInsecUsed = P / P1;
                float areaFertUsed = F / F1;
                if (areaInsecUsed > areaFertUsed) {
                    L = L - areaFertUsed;
                    F1 = 0;
                    totalWheatFertUsed = totalWheatFertUsed + F1;
                    areaOfWheat = areaOfWheat + areaFertUsed;
                    amount = amount + areaFertUsed * S1;
                } else if (areaInsecUsed < areaFertUsed) {
                    L = L - areaInsecUsed;
                    P1 = 0;
                    totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
                    areaOfWheat = areaOfWheat + areaInsecUsed;
                    amount = amount + areaInsecUsed * S1;
                }
            }

        }
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        return df.format(amount) + "," + df.format(areaOfWheat) + "," + df.format(areaOfRice);
    }

}
于 2020-06-25T17:46:25.667 回答
0
import java.text.DecimalFormat;
import java.util.Scanner;
public class CandidateCode {
    public static void main(String args[]) {
        System.out.println(get_total_profit());
    }
    public static String get_total_profit() {
        Scanner in = new Scanner(System.in);
        float L = in.nextInt();
        float F = in.nextInt();
        float P = in.nextInt();
        float F1 = in.nextInt();
        float P1 = in.nextInt();
        float F2 = in.nextInt();
        float P2 = in.nextInt();
        float S1 = in.nextInt();
        float S2 = in.nextInt();
        float totalRiceInsecUsed = 0f;
        float totalRiceFertUsed = 0f;
        float totalWheatInsecUsed = 0f;
        float totalWheatFertUsed = 0f;
        float areaOfWheat = 0.00f;
        float areaOfRice = 0.00f;
        float amount = 0.00f;
        while (true) {
            if ((L == areaOfRice + areaOfWheat) || P == totalRiceInsecUsed + totalWheatInsecUsed
                    || F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0 || F1 == 0 || P2 == 0 || P1 == 0) {
                break;
            }

            float calRiceProfit = Math.min(F / F2, P / P2) * S2;
            float calWheatProfit = Math.min(F / F1, P / P1) * S1;

            if (calRiceProfit > calWheatProfit) {
                float areaInsecUsed = P / P2;
                float areaFertUsed = F / F2;
                if (areaInsecUsed > areaFertUsed) {
                    L = L - areaFertUsed;
                    F2 = 0;
                    totalRiceFertUsed = totalRiceFertUsed + F2;
                    areaOfRice = areaOfRice + areaFertUsed;
                    amount = amount + areaFertUsed * S2;
                } else if (areaInsecUsed < areaFertUsed) {
                    L = L - areaInsecUsed;
                    P2 = 0;
                    totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
                    areaOfRice = areaOfRice + areaInsecUsed;
                    amount = amount + areaInsecUsed * S2;
                }
            } else {
                float areaInsecUsed = P / P1;
                float areaFertUsed = F / F1;
                if (areaInsecUsed > areaFertUsed) {
                    L = L - areaFertUsed;
                    F1 = 0;
                    totalWheatFertUsed = totalWheatFertUsed + F1;
                    areaOfWheat = areaOfWheat + areaFertUsed;
                    amount = amount + areaFertUsed * S1;
                } else if (areaInsecUsed < areaFertUsed) {
                    L = L - areaInsecUsed;
                    P1 = 0;
                    totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
                    areaOfWheat = areaOfWheat + areaInsecUsed;
                    amount = amount + areaInsecUsed * S1;
                        }
            }

        }
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        return df.format(amount) + " " + df.format(areaOfWheat) + " " + df.format(areaOfRice);
    }

}
于 2020-07-09T14:52:26.633 回答