【算法题】判断一组不等式是否满足约束并输出最大差

  • A+
所属分类:编程茶楼


共计 2503 个字符,预计需要花费 7 分钟才能阅读完成。

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 标题:判断一组不等式是否满足约束并输出最大差 | 时间限制:1秒 | 内存限制:65536K | 语言限制:不限
 * 给定一组不等式,判断是否成立并输出不等式的最大差(输出浮点数的整数部分),要求:
 * 1)不等式系数为double类型,是一个二维数组;
 * 2)不等式的变量为int类型,是一维数组;
 * 3)不等式的目标值为double类型,是一维数组;
 * 4)不等式约束为字符串数组,只能是:">",">=","<","<=","=",例如,不等式组:
 * a11*x1+a12*x2+a13*x3+a14*x4+a15*x5<=b1;
 * a21*x1+a22*x2+a23*x3+a24*x4+a25*x5<=b2;
 * a31*x1+a32*x2+a33*x3+a34*x4+a35*x5<=b3;
 * <p>
 * 最大差=max{  (a11*x1+a12*x2+a13*x3+a14*x4+a15*x5-b1),   (a21*x1+a22*x2+a23*x3+a24*x4+a25*x5-b2),   (a31*x1+a32*x2+a33*x3+a34*x4+a35*x5-b3)  },类型为整数(输出浮点数的整数部分)
 * <p>
 * 输入描述:
 * 1)不等式组系数(double类型):
 * a11,a12,a13,a14,a15
 * a21,a22,a23,a24,a25
 * a31,a32,a33,a34,a35
 * 2)不等式变量(int类型):
 * x1,x2,x3,x4,x5
 * 3)不等式目标值(double类型):b1,b2,b3
 * 4)不等式约束(字符串类型):<=,<=,<=
 * <p>
 * 输入:a11,a12,a13,a14,a15;a21,a22,a23,a24,a25;a31,a32,a33,a34,a35;x1,x2,x3,x4,x5;b1,b2,b3;<=,<=,<=
 * <p>
 * 输出描述:
 * true 或者 false, 最大差
 * 示例1
 * 输入
 * 2.3,3,5.6,7,6;11,3,8.6,25,1;0.3,9,5.3,66,7.8;1,3,2,7,5;340,670,80.6;<=,<=,<=
 * 输出
 * false 458
 * 示例2
 * 输入
 * 2.36,3,6,7.1,6;1,30,8.6,2.5,21;0.3,69,5.3,6.6,7.8;1,13,2,17,5;340,67,300.6;<=,>=,<=
 * 输出
 * false 758
 *
 * @since 2022年4月28日
 */
public class M_N_T_14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();

        String[] strings = line.split(";");

        // 不等式组系数(double类型)
        double[][] a = new double[strings[strings.length - 1].split(",").length][strings[0].split(",").length];
        for (int i = 0; i < a.length; i++) {
            String[] les = strings[i].split(",");
            for (int j = 0; j < les.length; j++) {
                a[i][j] = Double.parseDouble(les[j]);
            }
        }

        // 不等式变量(int类型)
        Integer[] x = Arrays.stream(strings[a.length].split(",")).map(Integer::parseInt).collect(Collectors.toList()).toArray(new Integer[strings[0].split(",").length]);

        // 不等式目标值(double类型)
        Double[] b = Arrays.stream(strings[strings.length - 2].split(",")).map(Double::parseDouble).collect(Collectors.toList()).toArray(new Double[a.length]);

        // 不等式约束(字符串类型)
        String[] con = Arrays.stream(strings[strings.length - 1].split(",")).collect(Collectors.toList()).toArray(new String[a.length]);

        // 开始比较
        boolean bool = true;
        double max = 0;
        for (int i = 0; i < con.length; i++) {
            boolean bo;
            double temp;
            switch (con[i]) {
                case ">":
                    temp = getLeft(a[i], x) - b[i];
                    bo = temp > 0;
                    break;
                case ">=":
                    temp = getLeft(a[i], x) - b[i];
                    bo = temp >= 0;
                    break;
                case "<=":
                    temp = getLeft(a[i], x) - b[i];
                    bo = temp <= 0;
                    break;
                case "<":
                    temp = getLeft(a[i], x) - b[i];
                    bo = temp < 0;
                    break;
                default:
                    temp = getLeft(a[i], x) - b[i];
                    bo = temp == 0;
            }

            max = Math.max(temp, max);
            if (bool) {
                bool = bo;
            }
        }

        System.out.println(bool + " " + (int) max);
    }

    public static double getLeft(double[] aRight, Integer[] x) {
        double d = 0;
        for (int i = 0; i < x.length; i++) {
            d += aRight[i] * x[i];
        }
        return d;
    }
}
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: