Noip 2005 equivalent expression arithmetic __c

Source: Internet
Author: User
Tags arithmetic cas

This question is a classical arithmetic expression evaluation

Because there is only one unknown a, so we can choose to find a few numbers into a, and then evaluate to see if the same as the original formula

A long long may be in the middle, but the value of an equivalent expression is still the same in the case of the explosion.

The process of seeking arithmetic is also a very classical application of the stack

With two stacks, one save operation, one save number

Then there is a problem with the priority level between the symbols. At this time to the stack inside and outside the stack two to calculate see I handwritten a table



#include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <cstdlib&
Gt
#include <stack> #include <cmath> #define EPS 1e-6 using namespace std;
Char str1[55], str2[55];
Char s1[55], s2[55];
int nei[333], wai[333];
int Len;
Long Long tet[10] = {11, 15, 18, 25, 29, 30, 36, 50, 55, 70};
stack<char>optr;
Stack<long long>opnd;
BOOL Flag;
        Long long calculate (long long x, long y, char c) {switch (c) {case ' + ': return x + y;
        Case '-': return x-y;
        Case "*": return x * y;
        Case ' ^ ': long long tmp = 1;
        for (int i = 0; i < y; i++) tmp *= x;
    return TMP;
    } Char CMP (char A, char b) {if (Nei[a] > Wai[b]) return ' > ';
    else if (nei[a] = = Wai[b]) return ' = ';
Return ' < ';
    Long Long Gao (char *s, long long t) {int i = 0;
    Long Long num;
        while (S[i]!= ' # ' | | | optr.top ()!= ' # ') {num = 0; if (!flag) return-1;
            if (s[i] >= ' 0 ' && s[i] <= ' 9 ') {while (S[i] >= ' 0 ' && s[i] <= ' 9 ')
                {num *= 10;
                num + + s[i]-' 0 ';
            i++;
        } opnd.push (num);
            else if (s[i] = = ' a ') {num = t;
            Opnd.push (num);
        i++; else {switch (CMP (optr.top (), s[i])) {case ' < ': Optr.push (S [i]); i++;
                Break Case ' = ': Optr.pop (); i++;
                Break
                    Case ' > ': if (Opnd.empty ()) {flag = false;
                return-1;
                Long Long ta = Opnd.top ();
                Opnd.pop ();
                    if (Opnd.empty ()) {flag = false;
                return-1;
         Long Long TB = Opnd.top ();       Opnd.pop ();
                Opnd.push (TB, TA, Optr.top ()) (calculate);
                Optr.pop ();
            Break
}} return Opnd.top ();
    } void Init () {while (!opnd.empty ()) Opnd.pop ();
    while (!optr.empty ()) Optr.pop ();
Optr.push (' # ');
    int main () {nei[' + '] = 2; wai[' + '] = 1; nei['-'] = 2;
    wai['-'] = 1; nei[' * '] = 4;
    wai[' * '] = 3; nei[' ^ '] = 6;
    wai[' ^ '] = 5; nei[') = 8;
    wai[') = 0;
    Nei[' ('] = 0; wai[' ('] = 8; nei[' # '] =-1;
    wai[' # '] =-1;
    Gets (STR1);
    len = 0;
    for (int i = 0; str1[i]; i++) if (Str1[i]!= ') s1[len++] = Str1[i];
    s1[len++] = ' # ';
    S1[len] = ' the ';
    int CAs;
    scanf ("%d", &cas);
    GetChar ();
        for (int i = 0; i < CAs; i++) {gets (STR2);
        len = 0;
        for (int j = 0; str2[j]; j +) if (Str2[j]!= ') s2[len++] = Str2[j];
        s2[len++] = ' # ';
        S2[len] = ' the ';
        flag = 1; for (int j = 0; J &LT 10;
            J + +) {init ();
            Long Long T1 = Gao (S1, Tet[j]);
            Init ();
            Long Long t2 = Gao (s2, tet[j]);
                if (T1!= t2) {flag = 0;
            Break
    } if (flag) printf ("%c", i + ' A ');
    printf ("\ n");
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.