湖南软件职业学院吧 关注:92,819贴子:3,200,321

懂C语言的进来看看吧

只看楼主收藏回复


古典问题:有一对兔子,从出生后起第3个月起每个月都生一对兔子,小兔子长到第三个月后起每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?(用C语音程序实现)
#include <stdio.h>
void main()
{
int i,num=0;
for(i=1;i<=12;i++)
{
if(i<=3)
{
num=num+2;
}
else
{
num=num+2*(i-3);
}
printf("%d ",num);
}
getch();
}
请问这样的思路对么? 懂的麻烦看看 求解 谢谢


1楼2012-03-07 21:05回复
    竟然你诚心诚意的发问了
    那我就大发慈悲的告诉你
    容我看2分钟


    2楼2012-03-07 21:18
    回复
      2025-11-12 17:27:16
      广告
      不感兴趣
      开通SVIP免广告
      呵呵 好的 慢慢看...


      3楼2012-03-07 21:19
      回复
        好像就是一个菲波拉契数列
        1 1 2 3 5 8 13..(单位:对)~~这样的数列


        4楼2012-03-07 21:26
        回复
          你的明显不对阿-.-
          if(i<=3)
          {
          num=num+2;
          }
          前2个月不生宝宝
          你这第二个月就让人家怀孕了
          人家多不好意思阿


          5楼2012-03-07 21:41
          回复
            lz错了,是等比数列啊
            #include<stdio.h>
            int sum(int i)
            { int num=1;
            while(i)
            { num*=2;
            i--; }
            return num;
            }
            void main()
            { int j,k;
            scanf("%d",&j);
            while(1)
            {
            if(j<=0)
            {
            printf("重输入!\n");
            scanf("%d",&j);
            }
            else
            break;
            }
            j=j/3;
            k=sum(j);
            printf("%d\n",k);
            }


            IP属地:广东6楼2012-03-07 22:08
            回复
              大姐
              这不是数学勒


              8楼2012-03-07 22:16
              回复
                刚跟妈妈打电话去了
                我以前写的是递归写的-+-..1年多没看C了-.-
                思路就是f(n)=f(n-1)+f(n-2) 月份小于3兔子数是不变的。。不是你那个num=num+2-.-!!!
                假如n=5的话
                fn(5)
                fn(4) + fn(3)
                (fn(3) + fn(2)) + (fn(2) + fn(1))
                ((fn(2) + fn(1)) + (fn(1) + fn(0))) + ((fn(1) + fn(0)) + fn(1))
                (((fn(1) + fn(0)) + fn(1)) + (fn(1) + fn(0))) + ((fn(1) + fn(0)) + fn(1))
                就是递归算法..
                


                10楼2012-03-07 22:33
                回复
                  2025-11-12 17:21:16
                  广告
                  不感兴趣
                  开通SVIP免广告
                  这就是数学的菲波拉契数列


                  11楼2012-03-07 22:33
                  回复
                    #include <stdio.h> void main()
                    {
                    int f1,f2,f3,i,month;
                    printf("Please input the month:\n");
                    scanf("%d",&month);
                    f1=1;
                    f2=1;
                    if(month>2&&month<=24){
                    for(i=3;i<=month;i++){
                    f3=f1+f2;
                    f1=f2;
                    f2=f3; }
                    printf("In the month,the rabbits number is:%d",f3); }
                    else{
                    printf("The rabbits number is 2");
                    }
                    getch();
                    }


                    14楼2012-03-07 22:52
                    回复
                      英语不好-.-
                      别笑
                      我写的单位是对。我看你的是一个一个-.-


                      15楼2012-03-07 22:53
                      回复