#include <iostream> #include <stack> #include <vector> using namespace std; int largestRectangleArea(vector<int>& heights) { stack<int> s; int maxArea = 0; int n = heights.size(); for (int i = 0; i <= n; i++) { while (!s.empty() && (i == n || heights[s.top()] >= heights[i])) { int height = heights[s.top()]; s.pop(); int width = s.empty() ? i : i - s.top() - 1; maxArea = max(maxArea, height * width); } s.push(i); } return maxArea; } int main() { vector<int> heights = {1, 1, 1, 1, 1, 1, 1, 1}; cout << largestRectangleArea(heights) << endl; return 0; }手动输入矩阵
时间: 2023-11-22 15:55:55 浏览: 63
#include <iostream>
抱歉,这段代码是计算给定柱状图中最大矩形面积的程序,并不需要手动输入矩阵。如果您想要编写手动输入矩阵的程序,可以参考以下示例代码:
```
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
cout << "请输入矩阵的行数和列数: ";
cin >> n >> m;
vector<vector<int>> matrix(n, vector<int>(m));
cout << "请输入矩阵元素: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
cout << "您输入的矩阵是: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
通过该程序,您可以输入矩阵的行数和列数,并手动输入矩阵的元素,最终输出您输入的矩阵。
阅读全文