题目分析:
3 = 1 + 2,4(不符合)
5 = 2 + 3, 6 = 1 + 2 + 3
7 = 3 + 4, 8(不符合)
9 = 4 + 5, 10 = 1 + 2 + 3 + 4
.......
在所有的数字中奇数都等于相邻两数的和 符合数字诗意
所有的2的幂都不满足数字诗意
所以只需判断非2的幂的偶数是否满足诗意即可
代码如下:
#include <iostream>
#include <cstdbool>
#include <vector>
// 判断一个数是否是2的幂次方
bool testMath(long long n) {
while (n % 2 == 0) {
n /= 2;
if (n == 1) {
return true;
}
}
return false;
}
// 判断不含诗意的数的数量
int getNonePoemMath(const std::vector<long long> nums) {
int count = 0;
for (long long num : nums) { //遍历数组nums中的数
if (testMath(num)) {
count++;
}
}
return count;
}
int main() {
int n;
std::cin >> n;
std::vector<long long> nums(n);
for (int i = 0; i < n; ++i) {
std::cin >> nums[i];
}
int result = getNonePoemMath(nums);
std::cout << result << std::endl;
return 0;
}