Saturday, 30 June 2012

UVa 10700 - Camel trading Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(LL i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007

LL multi_first(string line)
{
    stack<LL>stk;
    istringstream ss(line);
    LL num;
    LL mn=0;
    char c;
    ss>>num;
    stk.push(num);
    while(ss>>c)
    {
        if(c=='*')
        {
            LL x=stk.top();
            stk.pop();
            ss>>num;
            stk.push(num*x);
        }
        else
        {
            ss>>num;
            stk.push(num);
        }
    }

    while(!stk.empty())
        {
            mn+=stk.top();
            stk.pop();
        }
    return mn;

}

LL add_first(string line)
{
    stack<LL>stk;
    istringstream ss(line);
    LL num;
    LL mx=1;
    char c;
    ss>>num;
    stk.push(num);
    while(ss>>c)
    {
        if(c=='+')
        {
            LL x=stk.top();
            stk.pop();
            ss>>num;
            stk.push(num+x);
        }
        else
        {
            ss>>num;
            stk.push(num);
        }
    }

    while(!stk.empty())
        {
            mx*=stk.top();
            stk.pop();
        }
    return mx;

}


int main()
{
    LL mn,mx,tmp;
    LL t,num;
    char c;
    string line;
    cin>>t;
    getchar();
    while(t--)
    {
        getline(cin,line);

        //istringstream ss(line);
        //cout<<multi_first(line)<<endl;
        //cout<<add_first(line)<<endl;

        cout<<"The maximum and minimum are "<<add_first(line)<<" and "<<multi_first(line)<<"."<<endl;
    }
return 0;
}

No comments:

Post a Comment