矩阵连乘--栈 实现
来源:互联网 发布:站内优化 编辑:程序博客网 时间:2024/06/15 01:01
<span style="font-size:14px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">设有矩阵</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">M1,M2,M3,M4</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">,</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);"></span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">其维数分别是</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">10</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">×</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">20, 20</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">×</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">50, 50</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">×</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">1</span><span style="color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);"> </span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">和</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">1</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">×</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">100</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">,现要求出这</span><span lang="EN-US" xml:lang="EN-US" style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; background-color: rgb(226, 226, 226);">4</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 宋体; background-color: rgb(226, 226, 226);">个矩阵相乘的结果。</span></span>
<span style="font-size:18px;">用栈 实现矩阵连乘 问题</span>
#include <iostream>#include <cstring>#include <vector>#include <string>#include <stack>using namespace std;struct matrix{ int a; int b;}m[20];stack<matrix> s;int main(){ int n; cin>>n; for(int i=0;i<n;i++) { string name; cin>>name; int k=name[0]-'A'; cin>>m[k].a>>m[k].b; } string expr; while(cin>>expr) { int len=expr.length(); int ans=0; bool error=false; for(int j=0;j<len;j++) { if(isalpha(expr[j])) s.push(m[expr[j]-'A']); else if(expr[j] == ')') { matrix m1=s.top();s.pop(); matrix m2=s.top();s.pop(); if(m1.b != m2.a ){ error=true; break; } ans+=m1.a * m1.b * m2.b; //把 结果 新生成的矩阵 压入栈中 matrix tmp; tmp.a=m1.a; tmp.b=m2.b; s.push(tmp); } } if(error) printf("error\n"); else printf("%d\n",ans); } return 0;}
1 0
- 矩阵连乘--栈 实现
- 矩阵连乘算法java实现
- 矩阵连乘的Ruby实现
- C语言实现矩阵连乘问题
- 矩阵连乘问题 C语言实现
- C语言实现矩阵连乘算法
- 矩阵连乘问题的java实现
- JAVA实现矩阵连乘问题
- 动态规划实现矩阵连乘问题
- 动态规划--矩阵连乘问题
- 矩阵连乘
- Strassen矩阵连乘
- 矩阵连乘问题
- 矩阵连乘问题
- 矩阵连乘
- 矩阵连乘
- 矩阵连乘
- 矩阵连乘
- 155. Min Stack
- Qt属性系统
- Spring之IOC
- 聊聊高并发系统之限流特技
- 设计模式、架构与框架
- 矩阵连乘--栈 实现
- Ajax处理响应,连接 Web 表单
- 160. Intersection of Two Linked Lists
- angel arena reborn 装备合成说明
- JNIEnv 速查
- mysql数据备份
- 【Arduino官方教程第一辑】示例程序 6-2 用压电元件检测振动
- 士兵杀敌(一)nyoj
- Android源码分析之Builder模式