表达式求值
递归求解~~
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<algorithm> #include <vector> #include <queue> using namespace std; #define INF 0x3f3f3f3f #define N 310 char s[N]; int solve(int l, int r) { int t=0, index; for(int i=l; i<=r; i++) { if(s[i]=='(') t++; if(s[i]==')') t--; if(t==1&&s[i]==',') index=i; } if(s[l]=='a') return solve(l+4, index-1)+solve(index+1, r); else if(s[l]=='m'&&s[l+1]=='a') return max(solve(l+4, index-1), solve(index+1, r)); else if(s[l]=='m'&&s[l+1]=='i') return min(solve(l+4, index-1), solve(index+1, r)); else { int num=0; for(int i=l; i<=r; i++) if(isdigit(s[i])) num=num*10+(s[i]-'0'); return num; } } int main() { int T, len; scanf("%d", &T); while(T--) { scanf("%s", s); len=strlen(s); int ans=solve(0, len-1); printf("%d ", ans); } return 0; }