C++で初Bitsの出
最近C++成分が足りないので.
ここで問題。0 の三連続、[0,0,0] が最初に現れる位置は、平均で何ビット目でしょう?
初Bitsの出 -- d.y.d.
#include <iostream> #include <vector> #include <typeinfo> #include <ctime> #include <boost/range.hpp> #include <boost/range/adaptors.hpp> #include <boost/range/algorithm.hpp> #include <boost/assign.hpp> #include <boost/random.hpp> using std::cout; using std::endl; using namespace boost::assign; namespace { boost::mt19937 gen(static_cast<unsigned long>(time(0))); } template<typename RandomAccessRange> std::size_t first_occurence(RandomAccessRange const & rng) { using namespace boost; namespace range = boost::range; namespace adp = boost::adaptors; uniform_smallint<> dst(0, 1); variate_generator< mt19937&, uniform_smallint<> > rand(::gen, dst); std::vector<int> xs; while (size(xs) < size(rng) || ((xs | adp::sliced(size(xs)-size(rng), size(xs))) != rng)) { xs += rand(); //xs += std::rand() % 2; } } namespace { static size_t const TEST_COUNT = 10000; } void test (std::vector<int> const & xs) { unsigned int sum=0; for (size_t i=0; i<TEST_COUNT; ++i) sum += first_occurence(xs); cout << sum << " / " << static_cast<double>(sum)/TEST_COUNT << endl; } int main (void) { { std::vector<int> const xs = list_of(0)(0)(0); test(xs); } { std::vector<int> const xs = list_of(0)(0)(1); test(xs); } }
randomメンドクサイなー