C/C++

ПРОГРАММИРОВАНИЕ НА C++

Помогите с решением следующих задач

V(
Vladislav (16)
184
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
bool is_prime(int x) {
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 {
int n;
for (n = 3; n * n <= x && x % n; n += 2) { ; }
prime = n * n > x? true : false;
}
return prime;
}
bool is_fib(int n) {
if (n < 4) return true;
int a = 2, b = 3, ab = a + b;
while (ab <= n) {
if (ab == n) return true;
a = b;
b = ab;
ab = a + b;
}
return false;
}
int main() {
mt19937 gen{ random_device()() };
const int n = 12;
const streamsize w = 5;
const streamsize sp = 38;
int box[n];
cout <<'\n' << setw(sp) << "Task A:\n\n";
uniform_int_distribution<> uid_a(-10, 10);
for (auto& x : box) x = uid_a(gen);
for (const auto& x : box) cout << setw(w) << x;
puts("");
int box_a[n];
int na = 0;
for (auto& x : box) if (x < 0 && (~x & 1)) box_a[na++] = x;
for (int i = 0; i < na; ++i) cout << setw(w) << box_a[i];
cout << "\n\n" << setw(sp) << "Task B:\n\n";
uniform_int_distribution<> uid_b(0, 100);
for (auto& x : box) x = uid_b(gen);
for (const auto& x : box) cout << setw(w) << x;
puts("");
int box_b[n];
int nb = 0;
for (auto& x : box) if (is_prime(x)) box_b[nb++] = x;
for (int i = 0; i < nb; ++i) cout << setw(w) << box_b[i];
cout << "\n\n" << setw(sp) << "Task C:\n\n";
uniform_int_distribution<> uid_c(0, 35);
for (auto& x : box) x = uid_c(gen);
for (const auto& x : box) cout << setw(w) << x;
puts("");
int box_c[n];
int nc = 0;
for (auto& x : box) if (is_fib(x)) box_c[nc++] = x;
for (int i = 0; i < nc; ++i) cout << setw(w) << box_c[i];
puts("");
system("pause > nul");
}
Александр Бусс
Александр Бусс
93 237
Лучший ответ