[华为OD]判断一组不等式是否满足约束并输出最大差

#include 
#include 
#include 
using namespace std;

// 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
*/
int x1,x2,x3,x4,x5;
int res = 0;

map s2i = {
    {"<=", 1},
    {
        ">=", 2
    },
    {
        ">", 3
    },
    {
        "<", 4
    },
    {
        "=", 5
    }
    };

bool check(double a1, double a2, double a3, double a4, double a5, double b, string s) {
    res = max(res, int(a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4 + a5 * x5 - b));
    switch (s2i[s]) {
    case 1 :
        return a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4 + a5 * x5 <= b;
    case 2 :
        return a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4 + a5 * x5 >= b;
    case 3 :
        return a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4 + a5 * x5 > b;
    case 4 :
        return a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4 + a5 * x5 < b;
    case 5 :
        return a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4 + a5 * x5 == b;
    }
}

int main()
{
    string str;
    cin >> str;
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] == ',' || str[i] == ';') str[i] = ' ';
    }
    stringstream sin(str);
    double a11,a12,a13,a14,a15,a21,a22,a23,a24,a25,a31,a32,a33,a34,a35;
    double b1,b2,b3;
    string s1, s2, s3;

    sin >> a11 >> a12 >> a13 >> a14 >> a15
        >> a21 >> a22 >> a23 >> a24 >> a25
        >> a31 >> a32 >> a33 >> a34 >> a35
        >> x1 >> x2 >> x3 >> x4 >> x5
        >> b1 >> b2 >> b3
        >> s1 >> s2 >> s3;

    if (check(a11, a12, a13, a14, a15, b1, s1)
        && check(a21, a22, a23, a24, a25, b2, s2)
        && check(a31, a32, a33, a34, a35, b3, s3))
            cout << "true";
    else cout << "false";
    cout << " " << res << endl;
    return 0;
}

你可能感兴趣的:([华为OD]判断一组不等式是否满足约束并输出最大差)