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;i++)
 8 using namespace std;
 9 stack <long long>Max;
10 stack <long long>Min;
11 int main()
12 {
13     int N;
14     sc(N);
15     while(N--)
16     {
17         while(!Max.empty())
18         Max.pop();
19         while(!Min.empty())
20         Min.pop();
21         long long a, t, ans_max = 1, ans_min = 0;
22         sc1(a);
23         char ch;
24         Min.push(a);
25         Max.push(a);
26         while((ch = getchar()) != ‘\n‘)
27        {
28                sc1(a);
29                if(ch == ‘+‘){
30                Min.push(a);
31                t = Max.top();
32                Max.pop();
33                t += a;
34                Max.push(t);
35             }
36             else if(ch==‘*‘){
37                Max.push(a);
38                t = Min.top();
39                Min.pop();
40                t *= a;
41                Min.push(t);
42             }
43        }
44        while(!Min.empty())
45        {
46                ans_min += Min.top();
47                Min.pop();
48        }
49        while(!Max.empty())
50        {
51                ans_max *= Max.top();
52                Max.pop();
53        }
54         printf("The maximum and minimum are %lld and %lld.\n",ans_max,ans_min);
55     }
56     return 0;
57 }

这个是数学思路,需要用栈解决,同时输入字符是getchar();

通过一个一个字符输入,避免了字符串的输入,那时还要考虑转换成数组问题。

时间: 2024-10-18 19:42:45

UVA 10700 Camel trading的相关文章

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,

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(贪心)

题目: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

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 (归纳【好吧这是我自己起的名字】)——yhx

As you may know from the comic \Asterix and the Chieftain's Shield", Gergovia consists of one street,and every inhabitant of the city is a wine salesman. You wonder how this economy works? Simpleenough: everyone buys wine from other inhabitants of th

UVa 11054 Wine trading in Gergovia(扫描)

题意  有n个村庄  第i个村庄需要买a[i]的酒   a[i]为负时该村庄可卖掉-a[i]的酒  保证所有a[i]的和为0   一个单位的酒从一个村庄运输到相邻村庄的消耗为1  求运输完所有酒需要的最小消耗 总消耗最少时  每个需要买的村庄都会找离他最近的可以卖的村庄  容易发现  这种状况下  从第一个村和第二个村庄之间的运输量为abs(a[1])  第二个村庄和第三个村庄之间的运输量为abs(a[1]+a[2])   第k个村庄到第k+1个村庄的运输量为abs(a[1]+a[2]+...+

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的运输量,所以运输量不断累加或抵消,一直算到最右边村庄即可.