Dada una array, la tarea es barajar toda la array e imprimirla.
Ejemplo
Input (1, 2, 3, 4, 5, 6, 7} Output {3, 1, 6, 7, 2, 4, 5} Input (1, 2, 3} Output {3, 1, 2}
CPP
// C++ program to shuffle // the given array // using shuffle() method #include <bits/stdc++.h> using namespace std; // Shuffle array void shuffle_array(int arr[], int n) { // To obtain a time-based seed unsigned seed = 0; // Shuffling our array shuffle(arr, arr + n, default_random_engine(seed)); // Printing our array for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } // Driver code int main() { int a[] = { 10, 20, 30, 40 }; int n = sizeof(a) / sizeof(a[0]); shuffle_array(a, n); return 0; }
CPP14
// C++ program to shuffle // the given array // using random_shuffle() method #include <bits/stdc++.h> using namespace std; // Shuffle array void shuffle_array(int arr[], int n) { // To obtain a time-based seed unsigned seed = 0; // Shuffling our array using random_shuffle random_shuffle(arr, arr + n); // Printing our array for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } // Driver code int main() { int a[] = { 10, 20, 30, 40 }; int n = sizeof(a) / sizeof(a[0]); shuffle_array(a, n); return 0; }
Publicación traducida automáticamente
Artículo escrito por ShivamChauhan5 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA