题解
2025-01-04 19:39:54
发布于:浙江
2阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 检查给定的排列是否满足条件
bool check(const vector<int>& triangle, int P) {
int a = triangle[0], b = triangle[1], c = triangle[2], d = triangle[3];
int e = triangle[4], f = triangle[5], g = triangle[6], h = triangle[7], i = triangle[8];
// 条件1: a < f < i
if (!(a < f && f < i)) return false;
// 条件2: b < d, g < h, c < e
if (!(b < d && g < h && c < e)) return false;
// 条件3: a + b + d + f = f + g + h + i = i + e + c + a = p
if (!(a + b + d + f == P && f + g + h + i == P && i + e + c + a == P)) return false;
return true;
}
// 计算符合条件的三角形数目
int count(int p) {
vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int cnt = 0;
// 生成所有排列
do {
// 检查当前排列是否符合条件
if (check(nums, p)) {
cnt++;
}
} while (next_permutation(nums.begin(), nums.end())); // 获取下一个排列
return cnt;
}
int main() {
int p;
cin >> p;
int result = count(p);
if (result == 0) {
cout << "Not exist" << endl;
} else {
cout << result << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个