[求助]经典兔子问题
古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
那位好心人帮俺写上,俺想了好几天没想出来
----------------解决方案--------------------------------------------------------
去网上搜"经典程序百例"
----------------解决方案--------------------------------------------------------
#include "stdio.h"
#include "conio.h"
main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
{
printf("%12ld %12ld",f1,f2);
if(i%2==0) printf("\n"); /*控制输出,每行四个*/
f1=f1+f2; /*前两个月加起来赋值给第三个月*/
f2=f1+f2; /*前两个月加起来赋值给第三个月*/
}
getch();
}
----------------解决方案--------------------------------------------------------
上面好像没有体现"出生后第三个月起"的意思啊???
----------------解决方案--------------------------------------------------------
#include<iostream.h> void main(){ int months,i; long int firstmonth=1,secondmonth=0,adult=0; cin>>months; for(i=1;i<=months;i++){ cout<<"the "<<i<<"month ,the number of rubbit is"<<adult<<endl;
adult=adult+secondmonth; secondmonth=firstmonth; firstmonth=adult; cout<<"the total is "<<adult<<endl; }; }
不知行不?
----------------解决方案--------------------------------------------------------
呵呵我做出来了,还做了张图
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
void main(void)
{
int sum,r1=1,r2=0,r3=0;
int i,j,k;
int n;
int g=VGA,mode=VGAHI;
int x,y;
int x0,y0;
int *p;
clrscr();
printf("input the n:");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
if (p==NULL)
{
printf("\nwrong!\n");
exit(0);
}
printf("input the 3p:");
scanf("%f%f%f",&p1,&p2,&p3);
initgraph(&g,&mode,"");
setbkcolor(WHITE);
setcolor(BLUE);
line(150,380+5,150,50);
line(150,380,500,380);
moveto(150,50);
linerel(-3,6);
moveto(150,50);
linerel(3,6);
moveto(500,380);
linerel(-6,-3);
moveto(500,380);
linerel(-6,3);
for (j=0;j<=300;j+=25)
{
line(145,380-j,150,380-j);
}
for (k=0;k<=300;k+=25)
{
line(k+150,380,k+150,377);
}
for (i=2;i<=n;i++)
{
r3=r3+;
r2=r1;
r1=r3;
sum=r1+r2+r3;
x0=i*10;
y0=sum/2;
x=x0+150;
y=480-1*y0-100;
*(p+(i-2)*2)=x;
*(p+(i-2)*2+1)=y;
}
drawpoly(n,p);
getchar();
getchar();
getchar();
closegraph();
free(p);
}
----------------解决方案--------------------------------------------------------
main()
{
int i, ny,sum,n=3;
printf("please input the number of \'ny\':");
scanf("%d",&ny);
if(ny>=3)
{
sum=ny-3+2;
for(;n<ny;n++)
{
i=1;
while((ny-n)>=3*i)
{
sum+=ny-n-3*i+1;
i++;
}
}
}
else sum=1;
printf("%d\n",sum);
}
----------------解决方案--------------------------------------------------------
老谭那书里有这题啊
----------------解决方案--------------------------------------------------------