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 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<iostream>
#include<cstdio>
#include<vector>
#include<sstream>

using namespace std;
int n;
vector<long long> vmini,vmaxi;
string str;

void solve(){
    long long mina,maxa,a,b,maximum=1,minimum=0;
    stringstream ss;
    char ch;
    cin>>str;
    ss<<str;ss>>a;//流只能放在前面.
    mina=maxa=a;
    vmaxi.push_back(maxa);
    vmini.push_back(mina);
    while(ss>>ch>>b){
        if(ch=='+'){
            maxa=vmaxi.back()+b;
            vmaxi.pop_back();
            vmaxi.push_back(maxa);
            vmini.push_back(b);
        }
        else{
            mina=vmini.back()*b;
            vmini.pop_back();
            vmini.push_back(mina);
            vmaxi.push_back(b);
        }
    }
    for(int i=0;i<vmaxi.size();i++){//这样遍历更简单.
        maximum=maximum*vmaxi[i];
    }
     for(int i=0;i<vmini.size();i++){
        minimum=minimum+vmini[i];
    }
    printf("The maximum and minimum are %lld and %lld.\n",maximum,minimum);
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        vmaxi.clear();
        vmini.clear();
        solve();
    }
    return 0;
}

UVA 10700 Camel trading(计算式子加减乘除的优先级处理)

时间: 2024-10-13 22:24:42

UVA 10700 Camel trading(计算式子加减乘除的优先级处理)的相关文章

UVA - 10700 - Camel trading (简单贪心)

UVA - 10700 Camel trading Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem E - Camel trading Time Limit: 1 second Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formu

UVA 10700 Camel trading

1 #include <iostream> 2 #include <cstdio> 3 #include <stack> 4 #define sc(x) scanf("%d",&x) 5 #define sc1(x) scanf("%lld",&x) 6 #define pf(x) printf("%d\n",x) 7 #define FOR(i,b,e) for(int i=b;i<N;

uva:10700 - Camel trading(贪心)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这样的表达式加上括号后能得到的最大值和最小值. 解题思路:因为这些数的都是正整数,所以可以用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 因为比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> cons

uva:10700 - Camel trading(贪婪)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这种表达式加上括号后能得到的最大值和最小值. 解题思路:由于这些数的都是正整数,所以能够用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 由于比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> const

五一放假作业4.30 用正则表达式写一个计算器!去掉括号,计算式子结果!

作业要求:去掉括号,计算式子结果. 1 - 2 * ( (60-30 +(-9-2-5-2*3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ) #!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2017/4/30 7:44# @Author : Olivia Su# @File : 作业.py# @Softw

Problem E - Camel trading(栈和队列)

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 parenthesi

(白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)

题目地址:UVa 11054 很巧妙的一道题,这题是利用的等价转换,对每一条路来说,假如右边生产的比左边的多x,那么不管起点是哪,终点是哪,都可以把左右两侧的看成两个点,要从这条路上运送x个劳动力.再由于总和是0,所以只需要算出一端的总和就可以,这样只要遍历一遍就可以算出来了.写出代码就很简单了... 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.

UVA - 11054 Wine trading in Gergovia (Gergovia 的酒交易)(贪心+模拟)

题意:直线上有n(2<=n<=100000)个等距的村庄,每个村庄要么买酒,要么卖酒.设第i个村庄对酒的需求为ai(-1000<=ai<=1000),其中ai>0表示买酒,ai<0表示卖酒.所有村庄供需平衡,即所有ai之和等于0.把k个单位的酒从一个村庄运到相邻村庄需要k个单位的劳动力.计算最少需要多少劳动力可以满足所有村庄的需求. 分析:从最左面的村庄考虑,不管他是买酒还是卖酒,相对于他的相邻村庄都会有a0的运输量,所以运输量不断累加或抵消,一直算到最右边村庄即可.

UVA 11076 Add Again 计算对答案的贡献+组合数学

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, thenumber of di?erent integer pairs with LCM is equal to N