1. 程式人生 > >若一頭小母牛,從出生起第四個年頭開始每年生一頭母牛,按此規律,第n年有多少頭母牛?

若一頭小母牛,從出生起第四個年頭開始每年生一頭母牛,按此規律,第n年有多少頭母牛?

#include <stdio.h>
#include <stdlib.h>

int cow(int n)
{
    int i, count, m = 1;
    int A[3] = {0, 0, 0};
    int tmp;
    int pos;
    
    for (i = 0; i < n; i++) {
        pos = i % 3;
        tmp = A[pos];
        A[pos] = tmp + m;
        m = m + tmp;
        printf("year %d  1-%d, 2-%d, 3-%d --- %d\n",
                i, A[0], A[1], A[2],  m);

    }

    count = m;
    count += A[0] + A[1] + A[2];
   return count;    
}

int main(int argc, char *argv[])
{
    printf("\n%s Years cow num %d\n", argv[1], cow(atoi(argv[1])));
    return 0;
}