/*------------------------------------------------------------------------------ * problem019.c: Counting (6.6.3) **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #include #include #include /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define MAX_INT 100 /*------------------------------------------------------------------------------ * Type Definitions **----------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------ * Globals **----------------------------------------------------------------------------*/ static double Counts[MAX_INT + 1] = { 0, 2, 5, 13 }; /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ /* Count(1) = 2 * Count(2) = 5 * Count(3) = 13 * Count(n) = 2 * Count(n - 1) + Count(n - 2) + Count(n - 3) */ static double count(int n) { if (n > 3 && !Counts[n]) Counts[n] = 2*count(n - 1) + count(n - 2) + count(n - 3); return (Counts[n]); } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { int n; while (scanf("%d", &n) != EOF) printf("%.lf\n", count(n)); return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=cpp **----------------------------------------------------------------------------*/