数据结构的入门题,理解倒是不复杂,用两个栈就行(一个存数字一个存符号)。对于我这样的弱弱没事练练编码能力还是不错的。

注意运算优先级即可。(过两天回科大了,下次再做题也不知道何时,ACM生涯两铜收场orz)

题目链接:

http://hihocoder.com/contest/hihointerview11/problem/3

代码:

 #include <bits/stdc++.h>

 using namespace std;
typedef long long int64; stack<int64> sn;
stack<char> sc; const int maxn = + ;
char str[maxn]; char op[] = { '+', '-', '*', '/', '(', ')', '#' }; int getOpId(char op){
if(op == '+')
return -;
else if(op == '-')
return -;
else if(op == '*')
return -;
else if(op == '/')
return -;
else if(op == '(' )
return -;
else if(op == ')' )
return -;
else if(op == '#')
return -;
else if(op == '&')
return -;
} vector<int64> v; int getPrio(char op){
switch(op){
case '+': return ;
case '-': return ;
case '*': return ;
case '/': return ;
case '(': return ;
case '&': return ;
default: return ;
}
} int64 cal(int64 a, int64 b, char opt){
if(opt == '+'){
return a+b;
}else if(opt == '-'){
return a-b;
}else if(opt == '*'){
return a*b;
}else if(opt == '/'){
return a/b;
}
} int main(void){
scanf("%s", str);
int len = strlen(str);
str[len++] = '#'; for(int i = ; i < len; ++i){
if('' <= str[i] && str[i] <= ''){
int64 t = ;
while('' <= str[i] && str[i] <= ''){
t = t * + (str[i] - '');
i++;
}
i--;
v.push_back(t);
}else{
v.push_back(getOpId(str[i]));
}
//cout << v.back() << " ";
}
//cout << endl; int sz = v.size();
sc.push('&');
for(int i = ; i < sz; ++i){
if(v[i] >= ){
sn.push(v[i]);
//cout << "push " << v[i] << endl;
}else{
char c = op[-v[i]-];
if(c == '('){
sc.push('(');
//cout << "push " << '(' << endl;
}else if(c == ')'){
while(sc.top() != '('){
int64 b = sn.top(); sn.pop();
int64 a = sn.top(); sn.pop();
char opt = sc.top(); sc.pop();
sn.push(cal(a, b, opt));
//cout << "cal " << a << " " << opt << " " << b << endl;
}
sc.pop();
//cout << "pop " << '(' << endl;
}else{
int prio1 = getPrio(c);
int prio2; while(prio1 <= (prio2 = getPrio(sc.top()))){
//cout << "prio1: " << prio1 << " prio2: " << prio2 << endl;
int64 b = sn.top(); sn.pop();
int64 a = sn.top(); sn.pop();
char opt = sc.top(); sc.pop();
sn.push(cal(a, b, opt));
//cout << "cal " << a << " " << opt << " " << b << endl;
}
sc.push(c);
//cout << "push " << c << endl;
}
}
} //cout << "haha" << endl;
printf("%lld\n", sn.top()); return ;
} /**
5*(3+8*2)-(5+6*2-7)*2
100*(2+12)-(20/3)*2
(8+2*3/4-4)*(3-2+5/2*3)*(3-4+3)/6
*/

最新文章

  1. 【 2013 Multi-University Training Contest 2 】
  2. UML Sequence sample: if-else
  3. 获取 Chromium 源代码以及环境配置
  4. hdu4725最短路变形 添加点
  5. 20145212《Java程序程序设计》课程总结
  6. js框架简明
  7. Codeforces Round #177 (Div. 1) 题解【ABCD】
  8. linux获取目录下文件
  9. Android平台的四大天王:Activity, Service, ContentProvider, BroadcastReceiver
  10. JQ点击高亮显示
  11. chrom扩展学习
  12. margin负值-内秀篇
  13. node.js异步控制流程 回调,事件,promise和async/await
  14. CentOS系统搭建gitolite服务
  15. python打印万年历
  16. 安装好visual studio后,如何添加新的工作负载和组件
  17. 水晶报表Crystal 无效索引
  18. Yeoman安装和使用详解
  19. Redis 启动警告错误解决[转]
  20. 13.MD5对用户密码进行加密

热门文章

  1. listcontrol 加combobox
  2. Google Cloud SSH 连接配置
  3. Mac在python3环境下安装virtualwrapper遇到的问题
  4. 腾讯云,搭建nginx静态网站服务器
  5. BZOJ 1572 USACO 2009 Open 工作安排
  6. HTML的基本操作学习----常用标签,特殊符号,列表,表格,表单
  7. Spark源码值提交任务
  8. ACM的你伤不起
  9. Network Saboteur POJ 2531 回溯搜索
  10. 完全卸载VS2015的方法