the map. Instead use find.
if(n == 1) return 1;
if(n%2)
n = 3*n+1;
else
n = n/2;
cout << ' ' << n;
// shortcut to memorize steps[n]
steps[n] = compute_steps(n);
return steps[n] + 1;
}
int main(){
boost::filesystem::path file("record_h.txt");
unsigned int two[2];
if(exists(file)){
ifstream inf("record_h.txt", ios::binary);
while(inf.read((char *)two, 2*sizeof(unsigned int)))
steps[two[0]] = two[1];
inf.close();
}
int i, j;
// It's not as easy as it seems to safely read integers from cin
// The following trick makes sure a pair of integers are read in safely
while(!(cin >> i >> j)) {
cout << i << ' ' << j << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
assert(i != 0 && j >= i);
// compute steps for each l between i and l and prints its value
for(int l = i; l <= j; l ++){
cout << l << ": ";
steps[l] = compute_steps(l);
cout << '\n';
}
// compute some statitics of the result, the largest number n
steps[n] is computed
// the number of 0s from steps to steps+rbegin
unsigned int max_step = 0;
ofstream ofs("record_h.txt", ios::binary);
hash_map<unsigned int, unsigned int, hash<unsigned int>
::const_iterator it = steps.begin();
while(it != steps.end()){
two[0] = it->first;
two[1] = it->second;
max_step = (max_step > it->second) ? max_step : it->second;
ofs.write((const char *)two, 2*sizeof(unsigned int));
++it;
}
ofs.close();
cout << "maximum step: " << i << ' ' << j << ' ' << max_step << '\n';
}