uva 10700 Camel trading (水贪心+栈)

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.

——————————————————————————————
——————————————————————————————
给你一个式子,只包含+和*还有数字,数字不大于20,数字个数不超过12个,可以自由添加括号,求这个式子可以得到的最大值和最小值

++++++++++++++++++++++++++++++++++++++++++++
水贪心,能看出来求最小就是不填括号按照优先级计算的结果,求最大就是把加法和乘法的优先级调换,即 求最大先加后乘,求最小先乘后加

可以用个栈来实现,代码简单不少——————————当然我一开始没想到………………

————————————————————————————————————————————————————————————
贴上矬代码(:зゝ∠)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char formula[1000000],sym[1000000];
int n,num[1000000],tail,sl,syl,nul;
long long minimum,maximum,x;
int nuk[10000],nl,k;
long long mul[1000000],add[1000000];
int main()
{
    scanf("%d%*c",&n);
    for (int i=1;i<=n;i++)
    {
        gets(formula);
        minimum=0;
        maximum=1;
        //printf("%s\n",formula);
        memset(num,0,sizeof(num));
        memset(sym,'\0',sizeof(sym));
        sl=strlen(formula);
        nl=0;
        nul=0;
        syl=0;
        for (int i=0;i<sl;i++)
        {
            if (formula[i]=='+'||formula[i]=='*')
            {
                sym[++syl]=formula[i];
                nul++;
                k=1;
                while(nl)
                {
                    num[nul]+=(nuk[nl]*k);
                    k*=10;
                    nl--;
                }
                nl=0;
                /*printf("%d",num[nul]);
                printf("%c",sym[syl]);*/
            }
            else
            {
                nl++;
                nuk[nl]=formula[i]-48;
            }
        };
        nul++;
        k=1;
        while(nl)
        {
            num[nul]+=(nuk[nl]*k);
            k*=10;
            nl--;
        }
        tail=0;
        add[++tail]=num[1];
        for (int i=1;i<=syl;i++)
        {
            if (sym[i]=='+')
                add[++tail]=num[i+1];
            if (sym[i]=='*')
            {
                x=add[tail--];//x会爆_(:зゝ∠)_开long long
                x=x*num[i+1];
                add[++tail]=x;//add同理_(:зゝ∠)_
            }
        }
        for (int i=1;i<=tail;i++)
            minimum+=add[i];
        tail=0;
        mul[++tail]=num[1];
        for (int i=1;i<=syl;i++)
        {
            if (sym[i]=='*')
                mul[++tail]=num[i+1];
            if (sym[i]=='+')
            {
                x=mul[tail--];
                x=x+num[i+1];
                mul[++tail]=x;
            }
        }
        for (int i=1;i<=tail;i++)
            maximum*=mul[i];
        printf("The maximum and minimum are %lld and %lld.\n",maximum,minimum);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值