Информатика, вопрос задал doshazalupka52 , 2 года назад

Очень срочно!! Программы в с++

В игре «Русское лото" из мешка случайным образом выбираются бочонки, на каждом из которых написано число от 1 до 90. Напишите программу, которая выводит наугад пер- вые 5 номеров.


HarvesterReverser: А это че теперь в школах в 5-9 классах C++ учат? pepega

Ответы на вопрос

Ответил HarvesterReverser
0

Ответ:

// ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.

//

#include <iostream>

#include <vector>

#include <random>

using std::vector;

using std::cout;

using std::random_device;

using std::mt19937;

using std::uniform_int_distribution;

using std::endl;

int main()

{

   // объявляем костанты и переменные

   const int number_of_barrels = 90;

   const int number_of_random_numbers_to_generate = 5;

   const int range_from = 0;

   const int range_to = number_of_barrels;

   random_device rand_dev;

   mt19937 generator(rand_dev());

   vector<int> barrels;

   cout << "Filling vector." << endl;

   for(int i = 0; i < number_of_barrels; i++) {

       barrels.push_back(i+1);

   }

   cout << "Getting 5 random numbers from the vector and showing it on the screen:" << endl;

   for(int i = 0; i < number_of_random_numbers_to_generate; i++) {

       uniform_int_distribution<int> distr(range_from, range_to - i);

       int randomNumber = distr(generator); // генерируем рандомное число

       int numberAt = barrels.at(randomNumber); // читаем число из вектора и иницализируем переменную numberAt этим числом

       barrels.erase(std::cbegin(barrels) + randomNumber); // удаляем это число из вектора

       cout << numberAt << " "; // выводим это число на экран.

   }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu

// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:  

//   1. Use the Solution Explorer window to add/manage files

//   2. Use the Team Explorer window to connect to source control

//   3. Use the Output window to see build output and other messages

//   4. Use the Error List window to view errors

//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project

//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Объяснение:

В комментариях.

Приложения:

musicwave15: а использовать rand() не было бы проще?)
Новые вопросы