|
发表于 2023-11-13 19:35:49
|
显示全部楼层
- #include <iostream>
- #include <vector>
- using namespace std;
- // 判断一个整数是否为素数
- bool isPrime(int num) {
- if (num <= 1) {
- return false;
- }
- for (int i = 2; i * i <= num; i++) {
- if (num % i == 0) {
- return false;
- }
- }
- return true;
- }
- int main() {
- vector<vector<int>> nonPrimeArrays; // 存储所有连续的非素数数组
- int count = 0; // 连续非素数的个数
- vector<int> currentArray; // 当前的连续非素数数组
- for (int i = 2; i < 2000; i++) {
- if (!isPrime(i)) {
- currentArray.push_back(i);
- count++;
- if (count >= 10) {
- nonPrimeArrays.push_back(currentArray);
- }
- } else {
- count = 0;
- currentArray.clear();
- }
- }
- cout << "2000以内的有十个以上的所有连续的非素数数组:" << endl;
- for (const auto &arr : nonPrimeArrays) {
- for (int num : arr) {
- cout << num << " ";
- }
- cout << endl;
- }
- return 0;
- }
复制代码 |
|