//样例可以过,就是老提示运行错误,估计是内存神马的搞错了
//用了C++ stl 的栈和字符串类型,第一次用。//感觉还挺好吧,可惜就是没AC,不过纪念下。。。
#include<cstdio> #include<cstdlib> #include<string> #include<cmath> #include<stack> #include<sstream> #include<algorithm> #include<iostream> using namespace std; /* int map(string s) { int sum = 0; int t=s.length(); for(int i=t-1; i>=0; i--) sum+=(s[i]-'0')*(int)pow(10*1.0,t-1-i); return sum; }//转化为字符串 string convertToString(double x) { ostringstream o; if(o<<x) return o.str(); return error; //刚开始没有注意,以为可加可不加 }*/ string min(string x, string y) { if(x.compare(y)<=0) return x; return y; } string max(string x, string y) { if(x.compare(y)>=0) return x; return y; } string add(string x, string y) { int a=map(x); int b=map(y); a=a+b; return convertToString(a);}/* string add(string x, string y) { reverse(x.begin(), x.end()); reverse(y.begin(), y.end()); string s; for(int i=0; i<x.length()&&i<y.length(); i++) { s[i]=x[i]+y[i]; if(s[i]>=10) { s[i+1]=(s[i]-'0')/10+'0'; s[i]=(s[i]-'0')%10+'0'; } } if(i<x.length())s[i++]=x[i++]; if(i<y.length())s[i++]=y[i++]; reverse(s.begin(), s.end()); return s; } */ void camp(char *s) { string str; stack<char> S; str=s; int t = str.length(); for(int i=0; i<t; i++) if(str[i]==')') { char ch; string a, b, c, d; ch=S.top(); while(ch !=',') { a = a+ch; S.pop(); ch=S.top(); } S.pop(); ch=S.top(); while(ch!='(') { b = b+ch; S.pop(); ch=S.top(); } S.pop(); while(S.empty()!=true&&S.top()!=','&&S.top()!='(') { ch=S.top(); c = c+ch; S.pop(); // ch=S.top(); } reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); reverse(c.begin(),c.end()); if(c.compare("add")==0) d = add(a, b); else if(c.compare("max")==0) d = max(a, b); else d = min(a, b); for(int i=0; i<d.length(); i++) S.push(d[i]); } else S.push(str[i]); string d=""; while(S.empty()!=true) { d = d+S.top(); S.pop(); } reverse(d.begin(), d.end()); cout<<d<<endl; } int main() { char ss[3000]; int N; scanf("%d",&N); getchar(); while(N--) { scanf("%s",ss); camp(ss); } system("pause"); return 0; }
//下面是优秀的算法设计,递归输出。。。。。向他好好学习
01.
02.#include<cstdio>
03.#include<iostream>
04.using namespace std;
05.char str[1000];
06.int start;
07.int val()
08.{
09. int v,n;
10. switch(str[start])
11. {
12. case 'm':start+=3;if(str[start-2]=='i') return min(val(),val());else return max(val(),val());
13. case 'a':start+=3;return val()+val();
14. case ')':
15. case '(':
16. case ',':++start;return val();
17. default:sscanf(str+start,"%d%n",&v,&n);start+=n;return v;
18. }
19.}
20.int main()
21.{
22. int n;
23. scanf("%d",&n);
24. while(n--)
25. {
26. scanf("%s",str);
27. start=0;
28. printf("%d\n",val());
29. }
}
上一篇:
STL源码剖析——内存配置器alloc
下一篇:
最大子矩阵和问题 DP poj 1050