Página Principal

MC521-1s2014

MC621-2s2014

MC521-1s2015

MC621-2s2015

MC521-1s2016

Conteúdo

Área Reservada

edit sidebar

UVA 10700

Autor: Matheus Bellomo

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stack>

#define MAX 20

using namespace std;
typedef long long ll;

vector<int> v;
vector<char> s;
stack<ll> ansmax;
stack<ll> ansmin;

int n;

void calc_max_min(){
  ll auxmax;
  ll auxmin;

  ansmax.push(v[0]);
  ansmin.push(v[0]);
  for(int i = 0; i < (int)s.size(); i++){
    if(s[i] == '+'){
      ansmax.top() += v[i+1];
      ansmin.push(v[i+1]);
    } else{
      ansmax.push(v[i+1]);
      ansmin.top() *= v[i+1];
    }
  }

  auxmax = ansmax.top(); ansmax.pop();
  while(!ansmax.empty()){
    auxmax *= ansmax.top(); ansmax.pop();
  }

  auxmin = ansmin.top(); ansmin.pop();
  while(!ansmin.empty()){
    auxmin += ansmin.top(); ansmin.pop();
  }

  printf("The maximum and minimum are %llu and %llu.\n", auxmax, auxmin);
}


int main(){
  char c;
  int a;


  scanf("%d", &n);
  while(n--){
    scanf("%d", &a);
    v.push_back(a);
    c = getchar();
    while(c != '\n'){
      s.push_back(c);

      scanf("%d", &a);
      v.push_back(a);
      c = getchar();
    }

    calc_max_min();

    v.clear();
    s.clear();
  }


  return 0;
}