C++ 代码以查找容器中盒子占用面积
假设我们有 n 对需要装入正方形集装箱中的盒子。一对盒子的宽度表示为一对 (a, b),它们在一个数组“尺寸”中给出。如果我们使盒子宽度彼此平行,我们必须找出盒子在集装箱中占据多少面积。我们不能将盒子堆叠在一起。我们确定所有 n 对中两个盒子在集装箱中所需的最小面积。
因此,如果输入为 n = 4,dimensions = {{2, 4}, {3, 6}, {2, 5}, {4, 6}},则输出为 -
64 25 36 16
步骤
为了解决这个问题,我们将按照以下步骤操作 -
res := 0 while n is non-zero, do: a := first value of dimensions[n] b := second value of dimensions[n] res := maximum of (2 * minimum of (a, b) and maximum of a and b) print(res * res) n := n - 1
示例
让我们看一下以下实施,以获得更好的理解 -
#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, vector<pair<int, int>> dimensions) {
int res = 0;
while(n--){
int a = dimensions[n].first;
int b = dimensions[n].second;
int res = max(2 * min(a, b), max(a, b));
cout<< res * res << endl;
}
}
int main() {
int n = 4;
vector<pair<int, int>> dimensions = {{2, 4}, {3, 6}, {2, 5}, {4, 6}};
solve(n, dimensions);
return 0;
}输入
4, {{2, 4}, {3, 6}, {2, 5}, {4, 6}}输出
64 25 36 16
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP