题意:让你求运算式的结果和运算过程中加法的次数
(a) > (b) ? (a) : (b) 大于取a,小于等于取b
MAX( 1 + 2 , 3) 因为(a) > (b) ? (a) : (b) 所以取后面的值而在比较时进行了一次加法运算所以加法运算只有一次
MAX(3,1+2) 依旧取后面的值比较时进行一次加法运算,取后面的值还要进行一次加法运算,所以加法运算一共有两次
题解:数据结构的典型应用,两个栈一个存符号位,一个存数字,遇到')'进行一次运算
实现在数字栈中压入0,符号栈压入' ( ' , ' , ' 相当于预设的一次,最后再压入 ')' 进行最后的结果运算
总之,细心就能做出来吧……
自己还是太差了……!!!
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <cstdlib> #include <stack> #include <cctype> #include <string> #include <queue> #include <map> #include <set> using namespace std; const int INF = 0x7ffffff; const double ESP = 10e-8; const double Pi = 4 * atan(1.0); const int MAXN = 1000 + 10; const long long MOD = 1000000007; const int dr[] = {1,0,-1,0,-1,1,-1,1}; const int dc[] = {0,1,0,-1,1,-1,-1,1}; typedef long long LL; LL gac(LL a,LL b){ return b?gac(b,a%b):a; } char str[MAXN]; struct Point{ int num; int cnt; Point(int x = 0,int y = 0):num(x),cnt(y){} }; stack<Point>s1; stack<char>s2; int main(){ #ifndef ONLINE_JUDGE freopen("inpt.txt","r",stdin); // freopen("output.txt","w",stdout); #endif int t; while(~scanf("%d",&t)){ while(t--){ scanf("%s",str); while(!s1.empty()){ s1.pop(); } while(!s2.empty()){ s2.pop(); } s1.push(Point(0,0)); s2.push('('); s2.push(','); int len = strlen(str); /*!!!MAX(MAX(1+2,3),MAX(4+5+6,MAX(7+8,9)))+MAX(10,MAX(MAX(11,12),13))*/ for(int i = 0;i <= len;i++){ if(i == len){ str[i] = ')'; str[len+1] = '