题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
一方面考察矩阵乘法次数的计算,另一方面考察利用栈来解决运算优先级的问题。
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n = 0; cin >> n; vector<pair<int,int>> vec; for (int i = 0; i < n; i++) { int num1 = 0, num2 = 0; cin >> num1 >> num2; vec.push_back(make_pair(num1,num2)); } string s; stack<pair<int,int>> sta; cin >> s; int k = 0; int sum = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == ')') { auto first = sta.top(); sta.pop(); auto second = sta.top(); sta.pop(); int times = first.first * first.second * second.first; sum += times; pair<int,int> third = make_pair(second.first, first.second); sta.push(third); } else if (s[i] <= 'Z' && s[i] >= 'A') { sta.push(vec[k]); k++; } } cout << sum << endl; return 0; }