mt19937 gen { random_device()() }; uniform_int_distribution<> uid(0, 100); //диапазон от 0..100
int main() { fstream f("F2.txt"); int a, b; cin >> a >> b; for (size_t i = a; i < b; ++i) { f << uid(gen) << " "; } vector<int> v; map<int, size_t> m; int temp; while ( !f.eof() ) { f >> temp; v.push_back(temp); } for (auto it = v.begin(); it != v.end(); ++it) { m[*it]++; } for (const auto &i : m) { cout << i.first << " = " << i.second << endl; } }
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in;
ofstream out;
int a = 5, b = 15, c;
int k[b-a+1];
for (int i=0; i<b-a+1; i++) k[i] = 0;
in.open("f.txt");
while(in >> c)
{
k[c-a]++;
}
for (int i=0; i<b-a+1; i++)
cout << i+a << " - " << k[i] << '\n';
in.close();
return 0;
}
Пример:
Содержимое файла f.txt:
7 10 15
8 7 7 10
Результат:
5 - 0
6 - 0
7 - 3
8 - 1
9 - 0
10 - 2
11 - 0
12 - 0
13 - 0
14 - 0
15 - 1
#include <iostream>
#include <fstream>
#include <string>
#include <random>
#include <map>
#include <vector>
using namespace std;
mt19937 gen { random_device()() };
uniform_int_distribution<> uid(0, 100); //диапазон от 0..100
int main()
{
fstream f("F2.txt");
int a, b;
cin >> a >> b;
for (size_t i = a; i < b; ++i) {
f << uid(gen) << " ";
}
vector<int> v;
map<int, size_t> m;
int temp;
while ( !f.eof() ) {
f >> temp;
v.push_back(temp);
}
for (auto it = v.begin(); it != v.end(); ++it) {
m[*it]++;
}
for (const auto &i : m) {
cout << i.first << " = " << i.second << endl;
}
}
Пример:
55 13 51 55 55 21 38 59 62 20 62 10 9 55 28 60 28 24 95 7
7 = 1
9 = 1
10 = 1
13 = 1
20 = 1
21 = 1
24 = 1
28 = 2
38 = 1
51 = 1
55 = 4
59 = 1
60 = 1
62 = 2
95 = 1