用c++写程序:写满足下列条件的全部素数组2<=a<b<c<=100;a+c=2*b
时间: 2024-04-24 15:23:46 浏览: 103
下面是使用C++编写的程序,找出满足条件2 <= a < b < c <= 100,且 a + c = 2 * b 的所有素数组合:
```cpp
#include <iostream>
#include <vector>
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
std::vector<int> primes;
for (int i = 2; i <= 100; ++i) {
if (isPrime(i)) {
primes.push_back(i);
}
}
std::vector<std::vector<int>> primeTriples;
for (size_t i = 0; i < primes.size(); ++i) {
for (size_t j = i + 1; j < primes.size(); ++j) {
int a = primes[i];
int b = primes[j];
int c = 2 * b - a;
if (c > 100) {
break;
}
if (isPrime(c)) {
std::vector<int> triple = {a, b, c};
primeTriples.push_back(triple);
}
}
}
// 打印结果
for (const auto& triple : primeTriples) {
std::cout << triple[0] << ", " << triple[1] << ", " << triple[2] << std::endl;
}
return 0;
}
```
运行这个程序,它将输出满足条件的所有素数组合。希望这可以帮助到你!
阅读全文