C/C++

С++ вычисление последовательностей

Помогите сделать задачу.
Задача: Дано 3 последовательности. Определить в какой из них среднее арифметическое значение элементов больше.
#include <algorithm>
#include <vector>
#include <random>
#include <iterator>
#include <iostream>
#include <numeric>
using namespace std;
class Random {
public:
Random() {
random_device device;
random_generator.seed(device());
}
int next(unsigned first, unsigned last) {
uniform_int_distribution<unsigned> range(first, last);
return range(random_generator);
}
private:
mt19937 random_generator;
};
void fill(vector<unsigned>& box, Random rand, unsigned a, unsigned b) {
auto gen = [&] { return rand.next(a, b); };
generate(box.begin(), box.end(), gen);
}
void show(const char* msg, vector<unsigned>& box) {
cout << msg;
copy(box.begin(), box.end(), ostream_iterator<unsigned>(cout, " "));
cout.put('\n');
}
double average(vector<unsigned>& box) {
return accumulate(box.begin(), box.end(), .0) / box.size();
}
void show_value(const char* msg, double value) {
cout << msg << value << '\n';
}
double max(double a, double b, double c) {
return (a > b) ? (a > c) ? a : c : (b > c) ? b : c;
}
int main() {
const auto xa = 10U, xb = 20U;
const auto xl = 10U, xr = 50U;
Random rand;
auto sa = rand.next(xa, xb);
vector<unsigned> va(sa);
fill(va, rand, xl, xr);
show("va: ", va);
auto aa = average(va);
show_value("Average va: ", aa);
auto sb = rand.next(xa, xb);
vector<unsigned> vb(sb);
fill(vb, rand, xl, xr);
show("vb: ", vb);
auto ab = average(vb);
show_value("Average vb: ", ab);
auto sc = rand.next(xa, xb);
vector<unsigned> vc(sc);
fill(vc, rand, xl, xr);
show("vc: ", vc);
auto ac = average(vc);
show_value("Average vc: ", ac);
auto mx = max(aa, ab, ac);
if (mx == aa) puts("Max: va");
if (mx == ab) puts("Max: vb");
if (mx == ac) puts("Max: vc");
system("pause > nul");
}
Владимир Поляков
Владимир Поляков
54 877
Лучший ответ
1. #include
2. #include
3. #include
4. using namespace std;
5.
6. constexpr size_t size = 10;
7.
8. float max (float a, float b) { return a > b? a : b; }
9.
10. int main(void)
11. {
12. ios_base::sync_with_stdio(false);
13. cin.tie(nullptr);
14.
15. size_t size; cin >> size;
16.
17. random_device rd; mt19937 gen(rd());
18.
19. uniform_real_distribution<>dis(1, 99);
20.
21. vectora(size);
22.
23. vectorb(size);
24.
25. vectorc(size);
26.
27. float avg_a = 0, avg_b = 0, avg_c = 0;
28.
29. for(size_t i = 0; i < size; ++i)
30. {
31. a[i] = dis(gen);
32. b[i] = dis(gen);
33. c[i] = dis(gen);
34. avg_a += a[i];
35. avg_b += b[i];
36. avg_c += c[i];
37. }
38. avg_a /= size;
39. avg_b /= size;
40. avg_c /= size;
41.
42. cout << "A: " << avg_a << " B: " << avg_b << " C: " << avg_c << endl;
43. cout << "Max: " << max(avg_a, max(avg_b, avg_c)) << endl;
44.
45.
46.
47.
48.
49. cin.get();
50. return EXIT_SUCCESS;
51. }
Коля Солодко
Коля Солодко
59 715
Во втором
Tony Montana
Tony Montana
886
Радик Утямишев что во втором?
точно не знаю извините
#include
#include
#include
#include
#include
#include
using namespace std;
class Random {
public:
Random() {
random_device device;
random_generator.seed(device());
}
int next(unsigned first, unsigned last) {
uniform_int_distribution range(first, last);
return range(random_generator);
}
private:
mt19937 random_generator;
};
void fill(vector& box, Random rand, unsigned a, unsigned b) {
auto gen = [&] { return rand.next(a, b); };
generate(box.begin(), box.end(), gen);
}
void show(const char* msg, vector& box) {
cout << msg;
copy(box.begin(), box.end(), ostream_iterator(cout, " "));
cout.put('\n');
}
double average(vector& box) {
return accumulate(box.begin(), box.end(), .0) / box.size();
}
void show_value(const char* msg, double value) {
cout << msg << value << '\n';
}
double max(double a, double b, double c) {
return (a > b) ? (a > c) ? a : c : (b > c) ? b : c;
}
int main() {
const auto xa = 10U, xb = 20U;
const auto xl = 10U, xr = 50U;
Random rand;
auto sa = rand.next(xa, xb);
vector va(sa);
fill(va, rand, xl, xr);
show("va: ", va);
auto aa = average(va);
show_value("Average va: ", aa);
auto sb = rand.next(xa, xb);
vector vb(sb);
fill(vb, rand, xl, xr);
show("vb: ", vb);
auto ab = average(vb);
show_value("Average vb: ", ab);
auto sc = rand.next(xa, xb);
vector vc(sc);
fill(vc, rand, xl, xr);
show("vc: ", vc);
auto ac = average(vc);
show_value("Average vc: ", ac);
auto mx = max(aa, ab, ac);
if (mx == aa) puts("Max: va");
if (mx == ab) puts("Max: vb");
if (mx == ac) puts("Max: vc");
system("pause > nul");
}