hdu 3350

hdu 3350

题意:让你求运算式的结果和运算过程中加法的次数 

  (a) > (b) ? (a) : (b) 大于取a,小于等于取b

  MAX( 1 + 2 , 3) 因为(a) > (b) ? (a) : (b) 所以取后面的值而在比较时进行了一次加法运算所以加法运算只有一次

  MAX(3,1+2) 依旧取后面的值比较时进行一次加法运算,取后面的值还要进行一次加法运算,所以加法运算一共有两次

题解:数据结构的典型应用,两个栈一个存符号位,一个存数字,遇到')'进行一次运算

  实现在数字栈中压入0,符号栈压入' ( '  ,  ' , ' 相当于预设的一次,最后再压入 ')' 进行最后的结果运算

  总之,细心就能做出来吧……
  自己还是太差了……!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <queue>
#include <map>
#include <set>

using namespace std;

const int INF = 0x7ffffff;
const double ESP = 10e-8;
const double Pi = 4 * atan(1.0);
const int MAXN =  1000 + 10;
const long long MOD =  1000000007;
const int dr[] = {1,0,-1,0,-1,1,-1,1};
const int dc[] = {0,1,0,-1,1,-1,-1,1};
typedef long long LL;

LL gac(LL a,LL b){
   return b?gac(b,a%b):a;
}
char str[MAXN];
struct Point{
    int num;
    int cnt;
    Point(int x = 0,int y = 0):num(x),cnt(y){}
};
stack<Point>s1;
stack<char>s2;
int main(){
#ifndef ONLINE_JUDGE
    freopen("inpt.txt","r",stdin);
   // freopen("output.txt","w",stdout);
#endif
    int t;
    while(~scanf("%d",&t)){
        while(t--){
            scanf("%s",str);
            while(!s1.empty()){
                s1.pop();
            }
            while(!s2.empty()){
                s2.pop();
            }
            s1.push(Point(0,0));
            s2.push('(');
            s2.push(',');
            int len = strlen(str);
            /*!!!MAX(MAX(1+2,3),MAX(4+5+6,MAX(7+8,9)))+MAX(10,MAX(MAX(11,12),13))*/
            for(int i = 0;i <= len;i++){
                if(i == len){
                    str[i] = ')';
                    str[len+1] = '';
                }
                if(isalpha(str[i])){
                    s2.push('(');
                    i += 3;
                }
                else if(isdigit(str[i])){
                    int j = i;
                    int num = 0;
                    while(str[j] >= '0' && str[j] <= '9'){
                        num = num * 10 + str[j] - '0';
                        j++;
                    }
                    i = j-1;
                    s1.push(Point(num,0));
                }
                else if(str[i] == ',' || str[i] == '+'){
                    s2.push(str[i]);
                }
                else if(str[i] == ')'){
                    char ch = s2.top();
                    if(ch == '('){
                        s2.pop();
                        continue;
                    }
                    Point a = s1.top();
                    int num1 = a.num;
                    int cnt1 = a.cnt;
                    s1.pop();
                    while(ch == '+'){
                        s2.pop();
                        ch = s2.top();
                        Point b = s1.top();
                        s1.pop();
                        cnt1 += b.cnt;
                        cnt1++;
                        num1 += b.num;
                    }
                    s2.pop();
                    a = s1.top();
                    int num2 = a.num;
                    int cnt2 = a.cnt;
                    s1.pop();
                    ch = s2.top();
                    while(ch == '+'){
                        s2.pop();
                        ch = s2.top();
                        Point b = s1.top();
                        s1.pop();
                        cnt2 += b.cnt;
                        cnt2++;
                        num2 += b.num;
                    }
                    int tt = 0;
                    if(num2 > num1){
                        tt = cnt2;
                        if(i != len)
                            tt *= 2;
                        tt += cnt1;
                        s1.push(Point(num2,tt));
                    }
                    else{
                        tt = cnt1;
                        if(i != len)
                             tt *= 2;
                        tt += cnt2;
                        s1.push(Point(num1,tt));
                    }
                    s2.pop();
                }
            }
            printf("%d %d
",s1.top().num,s1.top().cnt);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hanbinggan/p/4450174.html