set需要自定义struct的排序方式
例如,定义一个struct如下:
struct tri
{
int x, y, z;
friend bool operator < (const tri &a, const tri &b)
{
if (a.x == b.x)
{
if (a.y == b.y) return a.z < b.z;
else return a.y < b.y;
}
else return a.x < b.x;
}
};
此后才能定义元素类型为tri的set:
set<tri> s;
Gym - 101510A Art
思路:对三角形三边排序,从小到大分别为x, y, z
x + y > z则可构成三角形
并将x,y,z作为tri类型,整体插入到set中,从而计算不重复的三角形的个数
C++代码如下:
// 坑:set需要自定义元素排序规则
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 10;
int a[maxn];
struct tri
{
int x, y, z;
friend bool operator < (const tri &a, const tri &b)
{
if (a.x == b.x)
{
if (a.y == b.y) return a.z < b.z;
else return a.y < b.y;
}
else return a.x < b.x;
}
};
set<tri> s;
tri t;
int b[4];
int main()
{
for (int i = 0; i < 5; i++) cin >> a[i];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
for (int k = 0; k < 5; k++)
{
if (i != k && i != j && k != j)
{
b[0] = a[i]; b[1] = a[j]; b[2] = a[k];
sort(b, b + 3);
if (b[0] + b[1] > b[2])
{
t.x = b[0]; t.y = b[1]; t.z = b[2];
s.insert(t);
}
}
}
}
cout << s.size() << endl;
return 0;
}