码迷,mamicode.com
首页 > 其他好文 > 详细

Problem E - Camel trading(栈和队列)

时间:2015-02-02 18:14:43      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

Description

技术分享

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 12numbers, 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<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

char a[1000100];

long long int min1;
long long int max1;

void minn()
{
    queue<long long int>q1;
    long long int num = 1;
    int p;
    long long int sum = 0;
    for(int i=0; a[i]!=\0; i++)
    {
        if(a[i]>=‘0‘ && a[i]<=‘9‘)
        {
            long long int p = a[i] - ‘0‘;
            sum = sum * 10 + p;
        }
        else
        {
            if(a[i] == ‘+‘)
            {
                sum = sum * num;
                q1.push(sum);
                sum = 0;
                num = 1;
            }
            else if(a[i] == ‘*‘)
            {
                num = num * sum;
                sum = 0;
            }
        }
    }
    q1.push(sum*num);
    min1 = 0;
    while(!q1.empty())
    {
        num = q1.front();
        min1 = min1 + num;
        q1.pop();
    }
}

void maxx()
{
    queue<long long int>q2;
    long long int sum = 0;
    long long int num = 0;
    for(int i=0; a[i]!=\0; i++)
    {
        if(a[i]>=‘0‘ && a[i]<=‘9‘)
        {
            long long int p = a[i] - ‘0‘;
            sum = sum *10 + p;
        }
        else
        {
            if(a[i] == ‘*‘)
            {
                sum = sum + num;
                q2.push(sum);
                sum = 0;
                num = 0;
            }
            else if(a[i] == ‘+‘)
            {
                num = num + sum;
                sum = 0;
            }
        }
    }
    q2.push(num+sum);
    max1 = 1;
    while(!q2.empty())
    {
        num = q2.front();
        max1 = max1 * num;
        q2.pop();
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        scanf("%s",a);
        minn();
        maxx();
        printf("The maximum and minimum are %lld and %lld.\n",max1,min1);
    }
    return 0;
}

Problem E - Camel trading(栈和队列)

标签:

原文地址:http://blog.csdn.net/yeguxin/article/details/43409789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!