The implementation for exact() comes straight from the definition of
exact:
bool exact(void) {
return population(seasons()) == max_population;
}
This code assumes the maximum population is stored in the private member
variable max_population.
In a solution that pre-computes the populations, there is a
somewhat easier and more efficient solution, requiring a private boolean member
variable is_exact, which is set after computing the populations:
max_season = 1;
while (pop[max_season] < max) {
max_season++;
pop[max_season] = pop[max_season - 2] + pop[max_season - 1];
}
is_exact = pop[max_season] == max;
The implementation of the public predicate exact() is
bool exact(void) {
return is_exact;
}
In any event, the remainder of the program is:
static void q(int s, int m) {
// Answer the question "Is the jackrabbit ecosystem with s initial rabbits
// and m maximum rabbits exact?"
jackrabbit_ecosystem jre(s, m);
cout << s << " " << m;
if (!jre.exact())
cout << " not";
cout << " exact\n";
}
int main() {
q(522, 1045);
q(74, 1000);
q(13, 100);
}
This page last modified on 3 July 2001.