1.完美数
暴力
#include <iostream>
#include <vector>using namespace std;bool perfect(long long res) {if (res <= 10) {return true;}else if (res > 10 && res <= 100 && res % 10 == 0) {return true;}else if (res > 100 && res <= 1000 && res % 100 == 0) {return true;}else if (res > 1000 && res <= 10000 && res % 1000 == 0) {return true;}else if (res > 10000 && res <= 100000 && res % 10000 == 0) {return true;}else if (res > 100000 && res <= 1000000 && res % 100000 == 0) {return true;}else if (res > 1000000 && res <= 10000000 && res % 1000000 == 0) {return true;}else if (res > 10000000 && res <= 100000000 && res % 10000000 == 0) {return true;}else if (res > 100000000 && res <= 1000000000 && res % 100000000 == 0) {return true;}return false;
}int main() {int n;cin >> n;vector<int> nums(n);for (int i = 0; i < n; i++) {cin >> nums[i];}int count = 0;for (int left = 0; left < n - 1; left++) {for (int right = left + 1; right < n; right++) {long long product = (long long)nums[left] * (long long)nums[right];if (perfect(product)) {count++;}}}cout << count << endl;return 0;
}
补充gpt
#include <iostream>
#include <vector>using namespace std;bool perfect(long long res) {bool has_non_zero_digit = false;while (res > 0) {int digit = res % 10;if (digit != 0) {if (has_non_zero_digit) {return false; // 如果已经有一个非零数字,且当前又有一个非零数字,则不是完美数}has_non_zero_digit = true;}res /= 10;}return has_non_zero_digit;
}int main() {int n;cin >> n;vector<int> nums(n);for (int i = 0; i < n; i++) {cin >> nums[i];}int count = 0;for (int left = 0; left < n - 1; left++) {for (int right = left + 1; right < n; right++) {long long product = (long long)nums[left] * (long long)nums[right];if (perfect(product)) {count++;}}}cout << count << endl;return 0;
}
2.可爱串
不会
3.好二叉树
奇数符合,偶数排除,0的时候有1种
#include <iostream>
#include <vector>using namespace std;const int MOD = 1e9 + 7;int main() {int n;cin >> n;vector<long long> dp(n + 1, 0);dp[0] = 1; for (int i = 1; i <= n; i += 2) {for (int j = 0; j <= i - 1; j++) {dp[i] = (dp[i] + dp[j] * dp[i - 1 - j]) % MOD;}}cout << dp[n] << endl;return 0;
}