统计三角形数组中相似三角形的组数

struct Triangle { Triangle(int _a = 0, int _b = 0, int _c = 0) { a = _a; b = _b; c = _c; } int a, b, c; }; inline void Swap(int &lhs, int &rhs) { int temp; temp = lhs; lhs = rhs; rhs = temp; } // // 将三角形的边长从小到大排序 ////////////////////////////////////////////////////////////////////////// void SortEdge(Triangle &tri) { if (tri.a > tri.b) { Swap(tri.a, tri.b); } if (tri.b > tri.c) { Swap(tri.b, tri.c); } if (tri.a > tri.b) { Swap(tri.a, tri.b); } } // // 判断两个三角形是否相似 ////////////////////////////////////////////////////////////////////////// bool IsSimilar(const Triangle &tri1, const Triangle &tri2) { int larger, smaller; larger = tri1.a; smaller = tri2.a; if (larger < smaller) Swap(larger, smaller); if (larger % smaller != 0) return false; larger = tri1.b; smaller = tri2.b; if (larger < smaller) Swap(larger, smaller); if (larger % smaller != 0) return false; larger = tri1.c; smaller = tri2.c; if (larger < smaller) Swap(larger, smaller); if (larger % smaller != 0) return false; return true; } // // 统计相似三角形的数目 ////////////////////////////////////////////////////////////////////////// int SimilarCount(Triangle* triArr, int size) { int count = 0; int *flagArr = new int[size]; memset(flagArr, 0, sizeof(int) * size); for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { SortEdge(triArr[i]); SortEdge(triArr[j]); if (IsSimilar(triArr[i], triArr[j]) && flagArr[i] ==0 && flagArr[j] == 0) { count++; flagArr[i] = flagArr[j] = 1; } } } delete []flagArr; return count; } int main(int argc, char* argv[]) { Triangle triArray[10] = { Triangle(2, 2, 3), Triangle(4, 4, 6), Triangle(9, 6, 6), Triangle(3, 4, 2), Triangle(3, 4, 2), Triangle(2, 5, 6), Triangle(4, 10, 12), Triangle(1, 1, 1), Triangle(2, 2, 2), Triangle(3, 3, 3) }; cout << SimilarCount(triArray, 10); return 0; }

你可能感兴趣的:(统计三角形数组中相似三角形的组数)