Right, this one definitely works (assuming my slowDayCount function is correct):

/** Given a run of days, length total_days, starting on
* start_day_of_week, where 0=Sunday, 6=Saturday, return the number of
* days that are a particular day, given by find_day_of_week
*/
int dayCount(int total_days, int start_day_of_week, int find_day_of_week)
{
if (total_days == 0)
return 0;

int total_weeks = total_days / 7;
int days_remaining = total_days % 7;

// If I've got entire weeks, then I've got one find_day_of_week in
// each.

// The run of days remaining starts at start_day_of week and ends
// at start_day_of_week + days_remaining - 1
if ((((find_day_of_week + 7) - start_day_of_week) % 7) < days_remaining)
return total_weeks + 1;

return total_weeks;
}

It's still got those two 'if' statements in it. Your call. I think that the problem with Bitt's solution (or, strictly, my implementation of it) is that the C/C++ % operator does odd things with negative numbers.


Attachments
144630-days.cpp (109 downloads)

_________________________
-- roger