首页 > 学院 > 开发设计 > 正文

uva 442矩阵链乘

2019-11-06 07:33:00
字体:
来源:转载
供稿:网友
#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <cctype>using namespace std;struct matrix{    int a, b;    matrix(int x = 0, int y = 0){//构造函数初始化a、b        a = x, b = y;    }}m[26];int main(){    stack<matrix> s;    int n; scanf("%d", &n);    for (int i = 0; i < n; i++) {        char ch;        getchar();//每次都接收一个换行        ch = getchar();        int temp = ch - 'A';        scanf("%d %d", &m[temp].a, &m[temp].b);    }    string exPR;    while (cin >> expr) {        int len = expr.length();        bool error = false;        int ans = 0;        for (int i = 0; i < len; i++) {            if (isalpha(expr[i])) s.push(m[expr[i] - 'A']);            else if (expr[i] == ')') {                matrix m2 = s.top(); s.pop();                matrix m1 = s.top(); s.pop();                if (m1.b != m2.a) {error = true; break;}                ans += m1.a*m1.b*m2.b;                s.push(matrix(m1.a, m2.b));            }        }        if (error) printf("error/n");        else printf("%d/n", ans);    }    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表