Witam Forumowiczów,
potrzebny mi jest program w którym po podaniu 10 liczb z zakresu od 1 do 80 zostanie wylosowanych 20 liczb,
a następnie program posortuje je dowolnym sposobem i sprawdzi ile było trafionych.
Z góry dziękuję za pomoc...
0
0
A do czego jest ci ten program potrzebny?
3
Cześć! W imieniu administracji witam Cię na tym forum. Pokaż proszę co już masz, abyśmy mogli Ci pomóc.
2
@gepard09:
Proszę, przerób pod swoje potrzeby.
int main()
{
unsigned counter = 0;
std::array<int, 10> numbers;
std::array<int, 20> randomNumbers;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(1,80);
std::iota(begin(numbers), end(numbers), 1);
std::transform(begin(randomNumbers), end(randomNumbers), begin(randomNumbers),
[&](int x){return dist(rng);});
std::sort(begin(randomNumbers), end(randomNumbers), std::greater<>());
std::for_each(begin(numbers), end(numbers),
[&](int x){(std::find(begin(randomNumbers), end(randomNumbers), x) != end(randomNumbers)) ? --counter : 0;});
}
3
Jeżeli użyjemy poczciwego random()
to można wyrobić się w trzech linijkach.
int main()
{
vector<int> input(istream_iterator<int>{cin},istream_iterator<int>{});
auto draw = [](){ set<int> r; generate_n( inserter( r , end(r) ) , 20 , [](){ return random()%80; } ); return r; }();
cout << count_if( begin(draw) , end(draw) , [&]( auto n ){ return find(begin(input),end(input),n) != end(input); } ) << endl;
}