C/C++

Написание программы C++ Массивы

#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
using namespace std;
class Numbers {
public:
vector<unsigned> prime()const {
vector<unsigned> box;
for (const auto& value : numbers) {
if (is_prime(value)) {
box.push_back(value);
}
}
return box;
}
vector<unsigned> fibonacci(const unsigned last) const {
vector<unsigned> box;
vector<unsigned> fib{ 0, 1 };
auto i = 1U;
auto number = fib.at(i - 1) + fib.at(i);
do {
fib.push_back(number);
++i;
number = fib.at(i - 1) + fib.at(i);
} while (number < last);
for (const auto& value : numbers) {
if (find(fib.begin(), fib.end(), value) != fib.end()) {
box.push_back(value);
}
}
return box;
}
void fill_random(const size_t length, unsigned a, unsigned b) {
Random rand;
if (a > b) swap(a, b);
for (auto i = 0U; i < length; ++i) numbers.push_back(rand.next(a, b));
}
bool is_prime(unsigned x)const {
bool prime;
if (x <= 1) prime = false;
else if (x <= 5 && (x <= 2 || x == 3 || x == 5)) prime = true;
else if (~x & 1 || 0 == x % 3 || 0 == x % 5) prime = false;
else {
unsigned n;
for (n = 3; n * n <= x && x % n; n += 2) { ; }
prime = n * n > x? true : false;
}
return prime;
}
void show(const char* msg)const {
puts(msg);
if (numbers.empty()) puts("Массив не содержит необходимых данных! \n");
else {
for (auto x : numbers) cout << x << ' ';
puts("\n");
}
}
private:
vector<unsigned> numbers;
class Random {
public:
Random() {
random_device device;
gen.seed(device());
}
unsigned next(unsigned first, unsigned last) {
uniform_int_distribution<unsigned> uid(first, last);
return uid(gen);
}
private:
mt19937 gen;
};
};
void show(const char* msg, const vector<unsigned>& box) {
puts(msg);
if (box.empty()) puts("Массив не содержит необходимых данных! \n");
else {
for (auto x : box) cout << x << ' ';
puts("\n");
}
}
int main() {
system("chcp 1251 > nul");
const auto length = 25U;
const auto left = 0U;
const auto right = 100U;
Numbers numbers;
numbers.fill_random(length, left, right);
numbers.show("Массив A:");
auto prime = numbers.prime();
show("Массив B (простые числа):", prime);
auto fibonacci = numbers.fibonacci(right);
show("Массив C (числа Фибоначчи):", fibonacci);
system("pause > nul");
}
АБ
Александр Борзых
53 974
Лучший ответ
Эти фибоначчи давно уже всем приелись хуже горькой редьки. А первое задание можно сделать так
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int p(int n) { int l, m;
if (n == 0 || n == 1) return 0;
if (n == 2) return 1; m=ceil(sqrt(n));
for (l = 2; l <= m; l++)
if (n % l == 0) return 0;
return 1; }
int main() { int i, m = 0, n; cout << "n = ";
cin >> n; int a[n]; srand(time(NULL)); cout << "A : \n";
for (i = 0; i < n; i++) { a[i] = rand()%101;
if (p(a[i])) m++; cout << a[i] << ' '; }
if (m == 0) cout << "\nNo primes\n";
else { int b[m]; m = 0; cout << "\nB :\n"; for (i = 0; i < n; i++)
if (p(a[i])) { b[m] = a[i]; cout << b[m] << ' '; m++; } }
Еркин Нуртазин
Еркин Нуртазин
29 440
Я на сис-админа пошёл или ты? Как ты мне роутер чинить будешь без этого после шаражки? Сам
S@sh@ Shr@m
S@sh@ Shr@m
4 488