湖南中医药 1402 蜂房 递归

01:湖南中医药 1402 蜂房

题解

#include <iostream>
using namespace std;//大神思想
int f(int start,int end){
    if(end==2)
        return 1;
    if(end==3)
        return 2;
    return f(1,end-1)+f(1,end-2);//从后往前遍历
}
int main(){
    int start,end;
    while(cin>>start>>end){
        cout<<f(1,end-start+1)<<endl;
    }
    return 0;
}

下面是我的初步思想,借助以上思想实现从前往后遍历:


import java.util.Scanner;

public class Main {	
	
	public static int F(int now,int end){
		if(now+1==end)return 1;
		if(now+2==end)return 2;
		if(now!=end){
		return F(now+1,end)+F(now+2,end);
		}
		return 0;
	}
	public static void main(String[] args) {
		
		int a,b;		
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			a=sc.nextInt();
			b=sc.nextInt();
			int end=b-a+1;				
			System.out.println(F(1,end));
		}
	}	 
}

由于做题少,还是不能深刻掌握

尽量通过教学视频和刷题来彻底解决递归。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值