hdu 3350

/*
hdoj 3350 栈的模拟题
MAX(1+1,2)+MAX(2,3)
读入整个字符串:
遇到MAX跳过,
1.遇到'(','+' 入符号栈 
2.遇到数字 入数字栈 
3,遇到','表示max的一半已经进入
    判断符号栈顶是否为'+', 是,就要弹出2个数字操作+
    
4,遇到')',表示max结束,就要操作比较大小
    但是左边仍然有可能有+的操作 ,
    所以,先要判断符号栈顶是否为'+', 是,就要弹出2个数字操作+ 
然后开始比较大小: 
    注意这里默认的','前后的+操作为0,
    若有一方为1,且是比较大的数,则操作是需要乘以2的
    MAX(1+2,2)==>max(3,1)==>3 操作了2次+,同下: 
    ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2  
最后结束 仍然要判断符号栈顶是否有+操作,就是max中间的+ 
*/
 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
 
struct number{
    int x,count;
};
 
stack<number> num_stack;
stack<char> oper;
 
char str[1001];
 
int main(){
    
    int t,len,i,y;
    number temp1,temp2,num;
    
//    freopen("test.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        len=strlen(str);
        
        for(i=0;i<len;i++)
        {
            if(str[i]>='0'&&str[i]<='9') //数字 提取完整入数字栈 
            {
                y=str[i]-'0';
                i++;
                while(str[i]>='0'&&str[i]<='9')
                {
                    y=y*10+str[i]-'0';
                    i++;
                }
                i--;
                num.count=0;
                num.x=y;
                num_stack.push(num);
            }
            else if(str[i]=='('||str[i]=='+')//( ,+ 符号 直接入符号栈 
                    {
                        oper.push(str[i]);
                    }
            else if(str[i]==',')//如果是‘,’,则把‘,’左边连续的‘+’进行运算, 
                                //运算结果重新压入栈中 
                {
                    while(!oper.empty()&&oper.top()=='+')
                    {
                        temp1=num_stack.top();
                        num_stack.pop();
                        temp2=num_stack.top();
                        num_stack.pop();
                        temp1.x+=temp2.x;
                        temp1.count+=temp2.count+1;
                        num_stack.push(temp1);
                        oper.pop();    
                    }                
                }
            else if(str[i]==')')//遇到‘)’,则说明有一个MAX 可以执行,
                                //但要先把左边连续的‘+’运算完
                {
                    while(!oper.empty()&&oper.top()=='+')
                    {
                        temp1=num_stack.top();
                        num_stack.pop();
                        temp2=num_stack.top();
                        num_stack.pop();
                        temp1.x+=temp2.x;
                        temp1.count+=temp2.count+1;
                        num_stack.push(temp1);
                        oper.pop();    
                    }
                    oper.pop();
                    temp2=num_stack.top();
                    num_stack.pop();
                    temp1=num_stack.top();
                    num_stack.pop();
                    if(temp1.x>temp2.x)
                        temp1.count=temp1.count*2+temp2.count;
                    else 
                    {
                        temp1.x=temp2.x;
                        temp1.count=temp1.count+temp2.count*2;
                    }
                    num_stack.push(temp1);                
                }
        }
        while(!oper.empty()&&oper.top()=='+')//完之后,还可能存在操作符没运算,
                                                //但只可能是‘+’
        {
                temp1=num_stack.top();
                num_stack.pop();
                temp2=num_stack.top();
                num_stack.pop();
                temp1.x+=temp2.x;
                temp1.count+=temp2.count+1;
                num_stack.push(temp1);
                oper.pop();    
        }
        printf("%d %d\n",num_stack.top().x,num_stack.top().count);
        num_stack.pop();    
    }
    return 0;
}

static Stack num = new Stack<>();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值