您好,欢迎来到叨叨游戏网。
搜索
您的当前位置:首页UVALive 6833 (模拟 水~)

UVALive 6833 (模拟 水~)

来源:叨叨游戏网

题意是给定一个表达式,求这个表达式是运用了哪种规则:

M:乘法优先

L:从左往右

U:ML都符合

I:都不符合


简单的模拟.

#include <bits/stdc++.h>
using namespace std;
#define maxn 111

char a[maxn], n;
long long num;

bool judge1 () {
    long long ans = a[0]-'0';
    for (int i = 1; i < n; i += 2) {
        if (a[i] == '+') {
            ans += a[i+1]-'0';
        }
        else ans *= a[i+1]-'0';
    }
    return ans == num;
}

vector <long long> gg;
bool judge2 () {
    gg.clear ();
    long long ans = 0;
    for (int i = 0; i < n; i += 2) {
        long long cur = a[i]-'0';
        if (a[i+1] == '*') {
            while (a[i+1] == '*') {
                cur *= (a[i+2]-'0');
                i += 2;
            }
        }
        gg.push_back (cur);
    }
    for (int i = 0; i < gg.size (); i++)
        ans += gg[i];
    return ans == num;
}

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%s", a) == 1) {
        scanf ("%lld", &num);
        n = strlen (a);
        bool ok1 = judge1 (), ok2 = judge2 ();
        if (ok1 && ok2) {
            printf ("U\n");
        }
        else if (!ok1 && !ok2) {
            printf ("I\n");
        }
        else if (ok2) {
            printf ("M\n");
        }
        else {
            printf ("L\n");
        }
    }
    return 0;
}


因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.net 版权所有 湘ICP备2024080961号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务