面试题:母牛生小母牛,N年之后牛的数量,5种写法

一头母牛从出生后,每两年可以生下一头母牛,即在第二年和第四年分别可产下一头母牛,出生后第五年将会死去。假设农场现有一头母牛,N年后农场的母牛数目是多少,编写程序实现
 
#include "stdio.h"
#include "windows.h"
long CowNum(int years);
long cow1(int years);
long cow2(int year);
int CowCntAftNyears(int n);
int GetCowNum(int N);
void main()
{
	int n = 50;
	DWORD start = GetTickCount();

	printf("%d ",CowNum(n) );
	printf("======CowNum=====%d\n" ,(start - GetTickCount() ));

	start = GetTickCount();
	printf("%d  ",cow1(n) );	
	printf("======cow1==========%d\n", (start - GetTickCount()));
	
	start = GetTickCount();
	printf("%d  ",cow2(n) );	
	printf("=====cow2===========%d\n", (start - GetTickCount()));

	start = GetTickCount();
	printf("%d  ",CowCntAftNyears( n) );
	printf("======CowCntAftNyears========%d\n", (start - GetTickCount()));

	start = GetTickCount();
	printf("%d  ",GetCowNum( n) );
	printf("======GetCowNum========%d\n", (start - GetTickCount()));

}
//基础  交易
long cow1(int years)
{
    long tab[5] = {1};
    long i, total = 1;
    for (i = 1; i <= years; ++i)
    {
        total += (tab[1] + tab[3] - tab[4]);
//      printf("%d years later total = %d\n", i, total);
        memmove(tab + 1, tab, (sizeof(tab) / sizeof(tab[0]) - 1) * sizeof(tab[0]));
        tab[0] = tab[2] + tab[4];
    }
    return total;
}

/*
将每一年的牛的数量都存到数组里面;
数组大小是5的整数倍+1( (N / 5 + 1) * 5 + 1 )
再返回对应发年份里面牛的个数
*/
long CowNum(int N)
{	
	int i = 0;
	long M = (N / 5 + 1) * 5 + 1 ;
	long *col =0;
	col = (long * )malloc(sizeof(long) * M);
	memset(col,0,sizeof(long)*M);
	col[0] = 1;
	col[i+1] = col[i];
	col[i+2] = 2*col[i];
	col[i+3] = col[i+2];
	col[i+4] = 2 * col[i+2] ;
	col[i+5] = col[i+4] - col[i];
	i = 6;

	while( i < M - 1)
	{
		if(i %2 ==1)
			col[i] = col[i-1] - col[i-5]/2;
		else
			col[i] = 2 * col[i-1];
		i++;
	}
	
	M = col[N];
	free(col);
	return M;

}
 
 
 
long cow2(int year)
{
    int v[] = {1,0,0,0,0,0};
    long sum = 0;
    int i ,j;
    for ( i = 0; i < year; ++i)
    {
        // 每循环一次 长一岁
        v[5] += v[4];//死掉的牛呀 可怜
        v[4] = v[3];
        v[3] = v[2];
        v[2] = v[1];
        v[1] = v[0];
        v[0] = v[2] +v[4]; //新牛上架 
    }

    for( j = 0 ; j < 5; ++j)
    {
        sum += v[j];
    }
    return sum;
}


int CowCntAftNyears(int n)
{
    int i,sum;
    int a[5]={1,0,1,0,2}; /* increment */
    if(n<0)return -1;
    switch(n)
    {
        case 0:
        case 1:
            return 1;
        case 2:
        case 3:
            return 2;
        case 4:
            return 4;
        default:
            for(i=5;i<=n;i++)
            {
                a[0]=a[1];
                a[1]=a[2];
                a[2]=a[3];
                a[3]=a[4];
                a[4]=a[0]+a[2];  /* 改了这一行 */
            }
            return a[0]+a[1]+a[2]+a[3]+a[4];
    }
}

//我认为比较简洁的代码了
int GetCowNum(int N)
{
	int x[5] = {1,0,0,0,0};
	int i,newCal;
    for( i = 1; i <= N; i++)
    {
        newCal = x[1]+x[3];
        x[4] = x[3];
        x[3] = x[2];
        x[2] = x[1];
        x[1] = x[0];
        x[0] = newCal;
    }
    return x[0]+x[1]+x[2]+x[3]+x[4];
}


 

 
 
 

你可能感兴趣的:(C语言)