Advertisement
Kolyach

Шарики

Dec 27th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <Windows.h>
  6. #include <ctime>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. void perestanovka(int begin, vector<int>& end) {
  12.     cout << "Перемешивание № " << begin << endl;
  13.     for (int i = 0; i < end.size(); i++) {
  14.         cout << end[i] << " ";
  15.     }
  16.     cout << endl;
  17.     int temp = 0;
  18.     for (int i = 0; i<end.size(); i++) {
  19.         if (i + 1 == end[i]) {
  20.             cout << "Совпадение с шаром № " << i + 1 << endl;
  21.             temp = end[i];
  22.             end[i] = end[begin - 1];
  23.             end[begin - 1] = temp;
  24.         }
  25.         else {
  26.             temp = end[i];
  27.             end[i] = end[begin - 1];
  28.             end[begin - 1] = temp;
  29.         }
  30.     }
  31.     cout << endl << endl << endl;
  32.  
  33.     if (begin != end.size() - 1) {
  34.         begin++;
  35.         perestanovka(begin, end);
  36.     }
  37.  
  38. }
  39. int main()
  40. {
  41.     SetConsoleCP(1251);
  42.     SetConsoleOutputCP(1251);
  43.     srand(time(NULL));
  44.     int temp = 0, n;
  45.     bool once = true;
  46.     cout << "Введите количество шаров" << endl;
  47.     cin >> n;
  48.     vector<int> balls(n, 0);
  49.     for (int i = 0; i < n; i++) {
  50.         while (balls[i] == 0) {
  51.             temp = rand() % n + 1;
  52.             for (int j = 0; j < n; j++) {
  53.                 if (temp == balls[j]) {
  54.                     once = false;
  55.                 }
  56.             }
  57.             if (once == true)
  58.                 balls[i] = temp;
  59.             once = true;
  60.         }
  61.     }
  62.     perestanovka(1, balls);
  63.     system("pause");
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement