优化:种植小麦和水稻

Optimization: Farming wheat and rice

这是问题陈述

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

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

你必须通过选择种植小麦and/or水稻的区域,找出农民可以获得的最大总利润。

例如:

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.

输入格式

唯一的输入将包含9个整数space-分隔,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

输出格式

输出将是

对于问题输出中考虑的示例将是 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。

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

这是一个线性优化问题 (https://en.wikipedia.org/wiki/Linear_programming) typically solved by the simplex algorithm (https://en.wikipedia.org/wiki/Simplex_algorithm)。

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

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

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

}