nyoj305
来源:互联网 发布:阿里云域名怎么绑定ip 编辑:程序博客网 时间:2024/06/05 10:41
题意:表达式求值
思路:栈的运用。
AC代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <stack>#include <queue>using namespace std;const int maxn=500;char ch[maxn];void calc(char *ch){ stack<int>s; //数字 stack<char>ss; // 运算符号 stack<char>sss; //比较符号 int len=strlen(ch); for(int i=0; i<len; i++){ if(ch[i]==',') continue; if(ch[i]=='m') continue; if(ch[i]=='a' && ch[i+1]=='d') { sss.push(ch[i+1]); i+=2; }else if(ch[i]=='i'){ sss.push(ch[i]); i+=1; }else if(ch[i]=='a' && ch[i+1]=='x'){ sss.push(ch[i]); i+=1; } else if(ch[i]=='('){ ss.push(ch[i]); } else if(ch[i]==')'){ int xx=s.top(); s.pop(); int yy=s.top(); s.pop(); if(sss.top()=='i'){ s.push(min(xx,yy)); }else if(sss.top()=='d'){ s.push(xx+yy); }else if(sss.top()=='a') { s.push(max(xx,yy)); } sss.pop(); ss.pop(); } else if(ch[i]>='0' && ch[i]<='9'){ int xx=0; while(ch[i]>='0' && ch[i]<='9'){ xx=xx*10+ch[i]-'0'; i++; } i=i-1; s.push(xx); } } printf("%d\n",s.top());}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%s",ch); calc(ch); } return 0;}
0 0
- nyoj305
- nyoj305
- nyoj305
- nyoj305
- 表达式求值(nyoj305)
- NYOJ305 表达式求值
- 表达式求值(nyoj305)
- NYOJ305表达式求值
- nyoj305 表达式求值(递归)
- NYOJ305 表达式求值(递归or栈)
- nyoj305表达式求值 栈的应用
- nyoj305表达式求值(数据结构---栈)
- nyoj305-表达式求值(栈。。dfs)
- 回顾 ——表达式求值——NYOJ305
- nyoj305表达式求值(第四届河南省程序设计大赛)
- NYOJ305 表达式求值(河南省第四届ACM省赛)
- neural artistic style艺术风格图片生成测试
- 百练+归并排序求逆序数+注意最后是按逆序数大小输出原来的序列啊!
- 【DP】最大子串和
- DLNA 笔记
- STL栈 - 火车进出栈
- nyoj305
- fzoj Problem 2190 非提的救赎 【单调栈】
- 【leetcode】226. Invert Binary Tree
- C++ 结构体指针的定义
- 深入了解viewport和px
- js ES3执行上下文
- 汇编语言第十三章-int中断
- windows 下VS2015配置opencv
- elasticsearch自定义分析器