华为OD机试真题-判断一组不等式是否满足约束并输出最大差-2023年OD统一考试(B卷)

题目描述:

给定一组不等式,判断是否成立并输出不等式的最大差(输出浮点数的整数部分),要求: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;

最大差=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)  },类型为整数(输出浮点数的整数部分)

输入描述:

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)不等式约束(字符串类型):<=,<=,<=

输入:a11,a12,a13,a14,a15;a21,a22,a23,a24,a25;a31,a32,a33,a34,a35;x1,x2,x3,x4,x5;b1,b2,b3;<=,<=,<=

输出描述:

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
说明:
 

import java.util.*;
 
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        ArrayList list = new ArrayList<>();
 
        String input = scanner.nextLine();
        String[] split = input.split(";");
        int len = split.length;
 
        for (int i = 0; i < len - 3; i++) {
            String[] temp = split[i].split(",");
            double[] arr = new double[temp.length];
            int index = 0;
            for (String s : temp) {
                arr[index++] = Double.parseDouble(s);
            }
            list.add(arr);
        }
 
        String[] xArr = split[len - 3].split(",");
        String[] bArr = split[len - 2].split(",");
        String[] condition = split[len - 1].split(",");
 
 
        int[] x = new int[xArr.length];
        int index = 0;
        for (String s : xArr) {
            x[index++] = Integer.parseInt(s);
        }
 
        double[] b = new double[bArr.length];
        index = 0;
        for (String s : bArr) {
            b[index++] = Double.parseDouble(s);
        }
 
 
        boolean flag = true;
        int max = Integer.MIN_VALUE;
 
        for (int i = 0; i < list.size(); i++) {
            double[] arr = list.get(i);
            int sum = 0;
 
            for (int j = 0; j < arr.length; j++) {
                sum += arr[j] * x[j];
            }
 
            // int temp = (int) Math.ceil(sum - b[i]);
            int temp = (int) (sum - b[i] + 0.6);
            max = Math.max(max, temp);
 
            switch (condition[i]) {
                case ">":
                    flag = flag && sum > b[i];
                    break;
                case ">=":
                    flag = flag && sum >= b[i];
                    break;
                case "<":
                    flag = flag && sum < b[i];
                    break;
                case "<=":
                    flag = flag && sum <= b[i];
                    break;
                case "=":
                    flag = flag && sum == b[i];
                default:
            }
        }
 
        System.out.println(flag + " " + max);
 
    }
}
import sys
def myans():
    tmp=list(map(lambda x:x.split(','),input().split(';')))
    s=tmp[-1]
    b=list(map(float,tmp[-2]))
    x=list(map(int,tmp[-3]))
    a=list(map(lambda x:list(map(float,x)),tmp[:-3]))
    ans=-sys.maxsize
    flag=True
    for i in range(3):
        count=0
        theb=b[i]
        sign=s[i]
        for j in range(5):
            count+=a[i][j]*x[j]
        if sign=='>':
            if not (count>theb):
                flag=False
        elif sign=='>=':
            if not (count>=theb):
                flag=False
        elif sign=='<':
            if not (count

 

你可能感兴趣的:(华为od)