Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const string holaMon = "Bon dia a tothom!";
  4.  
  5. /*
  6.  * Pre: cap
  7.  * Post: v conte una permutacio aleatoria dels elements inicials.
  8.  */
  9. void shuffle(vector<int>& v) {
  10.     for(int i=0; i<v.size(); ++i)
  11.         swap(v[rand()%v.size()], v[rand()%v.size()]);
  12. }
  13.  
  14. /*
  15.  * Pre: cap
  16.  * Post: Retorna una cadena la qual conte "Bon dia a tothom!" amb probabilitat 1/17!.
  17.  */
  18. string resultat(vector<int>& v) {
  19.     shuffle(v);
  20.     string ret;
  21.     for(int i=0; i<v.size(); ++i) ret += holaMon[v[i]];
  22.     return ret;
  23. }
  24.  
  25. int main() {
  26.         srand(time(NULL));
  27.         vector<int> v(holaMon.size());
  28.         for(int i=0; i<v.size(); ++i) v[i] = i;
  29.         string resposta;
  30.         while(resposta != holaMon)
  31.             resposta = resultat(v);
  32.         cout << resposta << endl;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement