UVA 10700 Camel trading

 

手推可以得到一定是先+后x的积最大,反之最小,然后模拟一遍即可,我的代码比较cuo,于是借鉴了别人用栈模拟优先级的代码,模仿才有创新。

 

CODE:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <stack>
using  namespace std;


stack< long  long> Max;
stack< long  long> Min;

void init()
{
     while(!Max.empty()) Max.pop();
     while(!Min.empty()) Min.pop();
}

int main()
{
     int T;
    scanf( " %d%*c ", &T);
     while(T--)
    {
        init();
         long  long  a;
        scanf( " %lld ", &a);
        Min.push(a), Max.push(a);
         char c;
         while((c = getchar()) !=  ' \n ')
        {
            scanf( " %lld ", &a);
             if(c ==  ' + ')
            {
                 long  long t = Max.top();
                Max.pop();
                t += a;
                Max.push(t);
                Min.push(a);
            }
             if(c ==  ' * ')
            {
                 long  long t = Min.top();
                Min.pop();
                t *= a;
                Min.push(t);
                Max.push(a);
            }
        }
         long  long ans1 =  1, ans2 =  0;
         while(!Max.empty())
        {
            ans1 *= Max.top();
            Max.pop();
        }
         while(!Min.empty())
        {
            ans2 += Min.top();
            Min.pop();
        }
        printf( " The maximum and minimum are %lld and %lld.\n ", ans1, ans2);
    }
     return  0;
}

 

 

你可能感兴趣的:(camel)