java递归练习

java递归练习

import java.util.*;
public class 递归练习 {
public static void main(String[] args) {
		int x=5;
		System.out.println(a(x));
		b(1,5);
		int [] qw= {5,2,1,3,4};
		int z=c(qw,0);
		System.out.println(z);
	String wee=	fz("abcd",3);
	System.out.println(wee);
	System.out.println(fbnq(8));		
	System.out.println(ojld(3,81));
}
	public static int a(int a) {//求阶乘
		if(a==1) 
			return 1;
	return a*a(a-1);
	}	
	public static void b(int a,int b) {//打印a~b
		if(a>b) 
			return;
		System.out.println(a);
		b(a+1,b);
	}
	public static int c(int[] a,int begin) {//求数组a所有值得和
		if(begin==a.length-1)
			return a[begin];
		return a[begin]+c(a,begin+1);	
	}
	public static String fz(String a,int end) {//字符串反转
		if(end==0)
			return ""+a.charAt(end);
		return a.charAt(end)+fz(a,end-1);
	}
	public static int fbnq(int n) {//输出斐波那契数列
		if(n==1||n==2)
			return 1;
		return fbnq(n-1)+fbnq(n-2);
	}
	public static int ojld(int a,int b) {//转转相除法(欧几里得求最大公约数法)
		if(a%b==0)
			return b;
		return ojld(b,a%b);
	}
}

以上代码是为了练习递归,为后续打下一个基础

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值