nyoj 305 表达式求值

第一次碰到这种题目,模板啊orz

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1002;
int pos, len;
char a[N];

int dfs()
{
	if( pos >= len )
		return 0;
	if( a[pos] == 'a' )
	{
		pos += 3;
		return dfs() + dfs();
	}
	else if( a[pos] == 'm' )
	{
		pos += 3;
		if( a[pos-2] == 'i' )
			return min( dfs(), dfs() );
		else
			return max( dfs(), dfs() );
	}
	else if( a[pos] == '(' || a[pos] == ')' )
	{
		pos++;
		return dfs();
	}
	else if( a[pos] == ',' )
	{
		pos++;
		return dfs();
	}
	else if( a[pos] >= '0' && a[pos] <= '9' )
	{
		int val = 0;
		while( a[pos] >= '0' && a[pos] <= '9' )
		{
			val = val * 10 + a[pos] - '0';
			pos++;
		}
		return val;
	}
}

int main()
{
	//freopen("test.in", "r", stdin);
	//freopen("qqq.txt", "w", stdout);
	int tot;
	scanf("%d", &tot);
	while( tot-- )
	{
		scanf("%s", a);
		pos = 0;
		len = strlen(a);
		int ans = dfs();
		printf("%d\n", ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值