これのC++版です。LLじゃないけど
特に変わったことはしてないけど、
h(100000)の計算が約0.3秒ですむという驚異。やはりC++、すばらしい(´▽`ノ
せっかくなのでLL2006の方にトラックバックしておきます :D
特に変わったことはしてないけど、
h(100000)の計算が約0.3秒ですむという驚異。やはりC++、すばらしい(´▽`ノ
#include <iostream>
#include <algorithm> //max_element
#include <map>
#include <cstdlib> // atoi
typedef std::map<int,int> xs_t;
int g(int n) {
return ( n == 1 ) ? 1 : 1 + g((n%2) ? n*3+1 : n/2);
}
int s(const xs_t::value_type& lhs, const xs_t::value_type& rhs) {
return lhs.second < rhs.second;
}
int h(int n) {
xs_t xs;
for (int i = 0; i < n; ++i) {
xs[i+1]=g(i+1);
}
return std::max_element(xs.begin(), xs.end(), s)->first;
}
int main(int argc, char** argv) {
int n = ( argc > 1) ? std::atoi(argv[1]) : 100;
std::cout << "h(" << n << ")=" << h(n) << std::endl;
return 0;
}
[追記]せっかくなのでLL2006の方にトラックバックしておきます :D




コメントする