Problem E - Camel trading

Problem E - Camel trading

Time Limit: 1 second

Background

Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the financial accounts of a camel transaction. The formula lacked parenthesis and was ambiguous. So, he decided to ask savants to provide him with a method to find which interpretation is the most advantageous for him, depending on whether is is buying or selling the camels.

The Problem

You are commissioned by El Mamum to write a program that determines the maximum and minimum possible interpretation of a parenthesis-less expression.

Input

The input consists of an integer N, followed by N lines, each containing an expression. Each expression is composed of at most 12 numbers, each ranging between 1 and 20, and separated by the sum and product operators + and *.

Output

For each given expression, the output will echo a line with the corresponding maximal and minimal interpretations, following the format given in the sample output.

Sample input

3
1+2*3*4+5
4*18+14+7*10
3+11+4*1*13*12*8+3*3+8

Sample output

The maximum and minimum are 81 and 30.
The maximum and minimum are 1560 and 156.
The maximum and minimum are 339768 and 5023.
 其实这题的思路很好想, 求最大值就定义‘+’的优先级高于'*', 求最小值的时候就定义‘*’的优先级高于'+'
就是知道也不一定做对,千万注意 数据的溢出问题啊
#include <stdio.h>
#include <string.h>
#include <math.h>
char s1[1000000];
long long int a[1000000];
char statck[1000000];
int main()
{
    int i,j,n,m,t,l;
    int top1,top2;
    long long int max,min,s;
    scanf("%d%*c",&t);
    while(t--)
    {
        gets(s1);
        l=strlen(s1);
        top1=top2=0;
        for(i=0;i<=l-1;)
        {
            if(s1[i]>='0'&&s1[i]<='9')
            {
                if((s1[i+1]>='0'&&s1[i+1]<='9'))
                {
                    a[top1++]=(long long int)((s1[i]-'0')*10+(s1[i+1]-'0'));
                    i+=2;
                }else
                {
                    a[top1++]=(long long int)(s1[i]-'0');
                    i++;
                }
            }else
            {
                if(top2==0)
                {
                    statck[top2++]=s1[i++];
                }else
                {
                    if(s1[i]=='*')
                    {
                        if(statck[top2-1]=='*')
                        {
                            statck[top2-1]=s1[i];
                            s=a[top1-1]*a[top1-2];
                            a[top1-2]=s;
                            top1--;
                        }else
                        {
                            statck[top2-1]=s1[i];
                            s=a[top1-1]+a[top1-2];
                            a[top1-2]=s;
                            top1--;
                        }
                    }else
                    {
                       if(statck[top2-1]=='*')
                        {
                           statck[top2++]=s1[i];
                        }else
                        {
                            statck[top2-1]=s1[i];
                            s=a[top1-1]+a[top1-2];
                            a[top1-2]=s;
                            top1--;
                        }
                    }
                    i++;
                }
            }
        }
        for(i=top2-1;i>=0;i--)
        {
            if(statck[i]=='*')
            {
                a[top1-2]=a[top1-1]*a[top1-2];
                top1--;
            }else
            {
                a[top1-2]=a[top1-1]+a[top1-2];
                top1--;
            }
        }
        max=a[0];
        top1=top2=0;
        for(i=0;i<=l-1;)
        {
            if(s1[i]>='0'&&s1[i]<='9')
            {
                if((s1[i+1]>='0'&&s1[i+1]<='9'))
                {
                    a[top1++]=(long long int)((s1[i]-'0')*10+(s1[i+1]-'0'));
                    i+=2;
                }else
                {
                    a[top1++]=(long long int)(s1[i]-'0');
                    i++;
                }
            }else
            {
                if(top2==0)
                {
                    statck[top2++]=s1[i++];
                }else
                {
                    if(s1[i]=='+')
                    {
                        if(statck[top2-1]=='*')
                        {
                            statck[top2-1]=s1[i];
                            s=a[top1-1]*a[top1-2];
                            a[top1-2]=s;
                            top1--;
                        }else
                        {
                            statck[top2-1]=s1[i];
                            s=a[top1-1]+a[top1-2];
                            a[top1-2]=s;
                            top1--;
                        }
                    }else
                    {
                       if(statck[top2-1]=='+')
                        {
                           statck[top2++]=s1[i];
                        }else
                        {
                            statck[top2-1]=s1[i];
                            s=a[top1-1]*a[top1-2];
                            a[top1-2]=s;
                            top1--;
                        }
                    }
                    i++;
                }
            }
        }
        for(i=top2-1;i>=0;i--)
        {
            if(statck[i]=='*')
            {
                a[top1-2]=a[top1-1]*a[top1-2];
                top1--;
            }else
            {
                a[top1-2]=a[top1-1]+a[top1-2];
                top1--;
            }
        }
        min=a[0];
        printf("The maximum and minimum are %lld and %lld.\n",max,min);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值