Created
February 17, 2015 12:03
-
-
Save anonymous/8c59f44ee3c03eec81dc to your computer and use it in GitHub Desktop.
test for http://habrahabr.ru/post/250817/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> // std::cout | |
#include <algorithm> // std::random_shuffle | |
#include <vector> // std::vector | |
#include <ctime> // std::time | |
#include <cstdlib> // std::rand, std::srand | |
bool experiment() | |
{ | |
std::vector<int> myvector; | |
for (int i=0; i<100; ++i) myvector.push_back(i); | |
std::random_shuffle ( myvector.begin(), myvector.end() ); | |
for (int i=0; i <100;i++) | |
{ | |
bool bFound = false; | |
int currentNumber = i; | |
for (int j = 0; j < 50; j++) | |
{ | |
if (myvector[currentNumber] == i) | |
{ | |
bFound = true; | |
break; | |
} | |
currentNumber = myvector[currentNumber]; | |
} | |
if (bFound == false) | |
return false; | |
} | |
return true; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
std::srand ( unsigned ( std::time(0) ) ); | |
long experiments = 0; | |
long succeededExperiments = 0; | |
for(;;) | |
{ | |
experiments++; | |
if (experiment()) | |
succeededExperiments++; | |
if (experiments % 10000 == 0) | |
std::cout << experiments << " - " << (double)succeededExperiments / experiments << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment