uva:10700 - Camel trading(贪婪)

题目:10700 - Camel trading

题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围【1,20】。这些表达式可能缺少了括号,问这种表达式加上括号后能得到的最大值和最小值。

解题思路:由于这些数的都是正整数,所以能够用贪心。不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法。注意这里的数值要用long long 由于比表达式的值可能会超过int。

代码:

#include <stdio.h>
#include <string.h>

const int N = 15;
char op[N];
char str[3 * N];
long long num[N];

long long caculate_max (int count) {

	long long temp[N];
	for (int i = 0; i < count; i++)
		temp[i] = num[i];

	for (int i = count - 2; i >= 0; i--)
		if (op[i] == '+') {

			temp[i] += temp[i + 1];
			temp[i + 1] = temp[i];
		}

	long long sum = temp[0];
	for (int i = 0; i < count - 1; i++) {

		if (op[i] == '*')
			sum *= temp[i + 1];

	}
	return sum;
}

long long caculate_min (int count) {

	long long temp[N];
	for (int i = 0; i < count; i++)
		temp[i] = num[i];

	for (int i = count - 2; i >= 0; i--) {

		if (op[i] == '*')
			temp[i] = temp[i] * temp[i + 1];
	}

	long long sum = temp[0];
	for (int i = 0; i <= count - 2; i++) {

		if (op[i] == '+')
			sum += temp[i + 1];
	}

	return sum;
}

int init () {

	int t = 0;
	long long sum;
	scanf ("%s", str);
	for (int i = 0; i < strlen (str); i++) {

		if (str[i] == '+' || str[i] == '*')
			op[t++] = str[i];
		else {

			sum = 0;
			while (str[i] >= '0' && str[i] <= '9') {

				sum = sum * 10 + str[i] - '0';
				i++;
			}
			num[t] = sum;
			i--;
		}
	}
	return t + 1;
}

int main () {

	int t;
	int count;
	scanf ("%d", &t);
	while (t--) {

		count = init();
		printf ("The maximum and minimum are %lld and %lld.\n", caculate_max(count), caculate_min(count));

	}
	return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-10-07 04:17:46

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

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

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