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