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;
}