1 条题解

  • 0
    @ 2023-6-11 12:16:30

    C :

    #include<stdio.h>
    int getTotalCount(int monthCount)
        {
            int count ;
            if(monthCount>=4){
                count = getTotalCount(monthCount-1)+getTotalCount(monthCount-2);
            }else if(monthCount==3){
                count = 2;
            }else {
                count =1;
            }
            return count;
        }
        
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	printf("%d",getTotalCount(n));
     } 
        
      
    

    C++ :

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    //求第n个月兔子的总数量,a(n) = a(n-1) + a(n-2) 
    int tuzi(int n){
    	int r = 0;
    	//交代起始项
    	if(n == 1 || n == 2){
    		r = 1;
    	} else{
    		r = tuzi(n - 1) + tuzi(n - 2); 	
    	}
    	
    	return r;
    }
    
    int main(){
    	int n,i;
    	cin>>n;
    	cout<<tuzi(n);
    }
    
    

    Python :

    def f(num):
        if num<1:
            return -1
        elif num==1 or num==2 :
            return 1
        else :
            return f(num-1)+f(num-2)
    a=int(input())
    r=f(a)
    if a!= -1:
        print(r)
    
    
    • 1

    【入门】统计每个月兔子的总数

    信息

    ID
    2198
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者