poj2246--数据结构栈
来源:互联网 发布:火箭引擎 知乎 编辑:程序博客网 时间:2024/05/21 10:47
简单的栈操作 对给定的输入串进行出栈如栈操作 每一次出栈后进行计算 得到的结果再次如栈 一直进行到输入串结束!!
#include<cstdio>#include<iostream>#include<cstring>#include<stack>#include<vector>#include<map>using namespace std;struct Node{ char name[10]; int row,col;};int main(){ int n; char exp[100]; Node maxtrix[30]; scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%s",maxtrix[i].name); scanf("%d%d",&maxtrix[i].row,&maxtrix[i].col); } memset(exp, '\0', sizeof(exp)); while(scanf("%s",exp) != EOF) { int i; int cou = 0; stack<Node> arr; for(i = 0; i < strlen(exp); i++) { if(exp[i] == '(') //不需要进行操作 直接进行下一个字符的判断 continue; if (exp[i] == ')') { //如果遇到')' 出现则要进行计算 //把最末尾的两个矩阵提取出来 Node b = arr.top(); arr.pop(); Node a = arr.top(); arr.pop(); //两个矩阵不能进行相乘的情况 if (a.col != b.row) { printf("error\n"); break; } cou += a.row * b.row * b.col; //进行一次出栈后的计算 结果再次如栈 Node tmp ; //tmp.name = a.name; tmp.row = a.row; tmp.col = b.col; arr.push(tmp); } else { //进行如栈操作 for(int j=0; j<n; j++) { if(maxtrix[j].name[0]==exp[i]) { arr.push(maxtrix[j]); break; } } } } if(i == strlen(exp)) printf("%d\n",cou); memset(exp, '\0', sizeof(exp)); } return 0;}
0 0
- poj2246--数据结构栈
- poj2246
- poj2246
- poj2246 Matrix Chain Multiplication (栈)
- POJ2246 水
- poj2246 stl
- poj2246 - Matrix Chain Multiplication
- poj2246 Matrix Chain Multiplication 矩阵连乘
- 数据结构---栈
- 数据结构-栈
- 数据结构 栈
- 数据结构--栈
- 数据结构 栈
- 数据结构(栈)
- 数据结构栈
- 数据结构-栈
- 数据结构-----栈
- 数据结构-栈
- poj3268
- ibatis排序无效的一种情况的解决办法
- java操作excel2010时无法找到WorkbookFactory类
- 在北京打拼的意义
- 南邮 OJ 1030 ACM程序设计之马拉松竞赛
- poj2246--数据结构栈
- c++阅读源代码方法
- Android中异步任务的操作
- IOS开发之实现App消息推送(最新)
- 关于如何利用CARRAY实现二维数组
- [深入理解Android卷二 全文-第三章]深入理解SystemServer
- Maven--项目构建工具的使用
- 从优化到再优化,最长公共子串
- [php] 获取serializeArray()提交的数据并处理