矩阵连乘--栈 实现

来源:互联网 发布:站内优化 编辑:程序博客网 时间: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
原创粉丝点击