博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
九度 1408 寻找表达式 (中缀转后缀)
阅读量:6869 次
发布时间:2019-06-26

本文共 2352 字,大约阅读时间需要 7 分钟。

 

总结

1. '_' 运算符不是 a*10 + b, 而是 a*(10 or 100) + b

2. char * 与 string 的相互转化

char* = string.c_str()

string = char*

3. 中缀表达式转后者表达式 参考:http://www.cnblogs.com/xinsheng/p/3591781.html

4. 通用的中缀转后缀, 用 string 存数据比较好

 

代码 未通过九度测试

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;string ops[3] = {" ", "+", "-"};map
priority;vector
> resVec;int n;string i2s(int i) { char s[20]; sprintf(s, "%d", i); string str = s; return str;}int s2i(string &str) { int tmp(atoi(str.c_str())); return tmp;}int calculate(vector
&vec) { vector
pos; deque
stack; for(int i = 0; i < vec.size(); i ++) { if(priority.count(vec[i]) == 0){ // is number pos.push_back(vec[i]); continue; } while(!stack.empty()) { string lastOp = stack.back(); if(priority[vec[i]]> priority[lastOp]) { stack.push_back(vec[i]); break; }else { pos.push_back(lastOp); stack.pop_back(); } } // is operator if(stack.empty()) { stack.push_back(vec[i]); continue; } } while(!stack.empty()) { pos.push_back(stack.back()); stack.pop_back(); } stack.clear(); for(int i = 0; i < pos.size(); i ++) { if(priority.count(pos[i]) == 0) { stack.push_back(pos[i]); }else { string sec = stack.back(); stack.pop_back(); string fir = stack.back(); stack.pop_back(); if(pos[i] == "+") { int ret = s2i(fir) + s2i(sec); stack.push_back(i2s(ret)); }else if(pos[i] == "-") { int ret = s2i(fir) - s2i(sec); stack.push_back(i2s(ret)); }else { int ret = s2i(fir)*pow(10, sec.size()) + s2i(sec); stack.push_back(i2s(ret)); } } } return s2i(stack.back());}void dfs(int i, vector
&party) { if(i == n) { party.push_back(i2s(n)); int val = calculate(party); if(val == 0) resVec.push_back(party); party.pop_back(); return; } party.push_back(i2s(i)); for(int j = 0; j < 3; j ++) { party.push_back(ops[j]); dfs(i+1, party); party.pop_back(); } party.pop_back();}int main() { //freopen("C:\\Users\\vincent\\Dropbox\\workplacce\\joj\\test.txt", "r", stdin); priority["+"] = 1; priority["-"] = 1; priority[" "] = 2; while(scanf("%d", &n) != EOF) { vector
temp; dfs(1, temp); for(int i = 0; i < resVec.size(); i ++) { for(int j = 0; j < resVec[i].size(); j ++) { cout << resVec[i][j]; } cout << endl; } } return 0;}

  

转载于:https://www.cnblogs.com/zhouzhuo/p/3642526.html

你可能感兴趣的文章
JDK1.8环境下依然报错 Unsupported major.minor version 52.0
查看>>
[Err] 1449 - The user specified as a definer ('rybhe'@'%') does not exist
查看>>
CSDN日报20170423 ——《私活,永远挽救不了自己屌丝的人生!》
查看>>
QGE 在齐次 Besov 空间中的准则
查看>>
遥感影像数据产品级别概述
查看>>
圆的内接三角形这样画最方便
查看>>
GoldenGate12.3中新增的Parallel Replicat (PR)介绍
查看>>
受限玻尔兹曼机——用在推荐系统里
查看>>
EBS已安装模块
查看>>
Android性能优化:布局优化 详细解析(含<include>、<ViewStub>、<merge>讲解 )
查看>>
UWP 手绘视频创作工具技术分享系列 - 手绘视频导出
查看>>
Python import容易犯的一个错误
查看>>
Dubbo,Zookeeper入门
查看>>
SecureCRT自动断开连接的问题
查看>>
leetcode 101. Symmetric Tree
查看>>
Bash - 趣味Shell
查看>>
鹅厂优文|打通小程序音视频和webRTC
查看>>
『Github』本地项目更新到服务器
查看>>
黄聪:iOS $299刀企业证书申请的过程以及细节补充
查看>>
Java并发编程的艺术(一)——并发编程需要注意的问题
查看>>