bool increment_datetime( char* stamp, int digit = 0, int ix = 0, int month = 0, char lim = '9') { char nxlim = (digit == 5 && (month = ((stamp[ix - 1] * 10 + stamp[ix]) & 0xf)) == 2) ? ('2') : (!(digit & 1) && stamp[ix] < lim) ? ('9') : (digit == 6) ? ("181010110101"[month - 1] + (month == 2 && (((stamp[2]<<1) ^ stamp[3]) & 3) == 0 && ((stamp[2]|stamp[3]) != '0' || !(((stamp[0]<<1) ^ stamp[1]) & 3)))) : ("99991231235959"[digit + 1]); int nx = ix + 1 + (unsigned(stamp[ix + 1] - '0') > 9); if (digit == 13 || increment_datetime(stamp, digit + 1, nx, month, nxlim)) { if (stamp[ix] == lim) { stamp[ix] = "00000101000000"[digit]; return true; } ++stamp[ix]; stamp[nx] &= ~'\1'; } return false; } #include #include int main() { char stamp[20]; while (cin.getline(stamp, sizeof(stamp))) { increment_datetime(stamp); std::cout << stamp << std::endl; } return 0; } /* try these 19991231235959 19990228235959 20000228235959 20040229235959 20040228235959 20041228235959 */