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;
}

 

 

转载于:https://www.cnblogs.com/g0feng/archive/2012/10/16/2726232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值