php – How do you compare the sizes of two boxes?

Question:

Given two boxes with dimensions (L, B, H). We need to write a nice (speed-optimized) size comparison function sizes_compare(l1, b1, h1, l2, b2, h2):booleean , function sizes_compare(l1, b1, h1, l2, b2, h2):booleean , output: equal / not equal. Considering that boxes (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2) and (3,2,1 ) are the same (just inverted in space).

Something I do well, it turns out very long. It was not possible to come up with a hash function, collisions slip through. By direct comparison, we get the Šaghetti code. If you think of it as two arrays, sort and compare, it's somehow too tricky …

language is not important, ideas of how this can be optimally implemented are interesting. Used in a critical place, optimization for execution speed is needed.

Answer:

Sorting and comparing sequentially is the most normal option.

In this case, you can sort both by sorting the array, and simply by hand.

https://ideone.com/d9i6xm

#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
  int a[3], b[3];

  scanf("%d%d%d%d%d%d", a, a+1, a+2, b, b+1, b+2);

  sort(a, a+3);
  sort(b, b+3);

  puts(a[0]==b[0] && a[1]==b[1] && a[2]==b[2] ? "YES" : "NO");

  return 0;
}
Scroll to Top