当前位置:网站首页>递归相关习题
递归相关习题
2022-07-27 23:36:00 【Wmpreturn】
阶乘n!
public static int factorial(int n)
{
if(n == 0) return 1;
return n*factorial(n-1);
}
Fibonacci数列
public static int fibonacci(int n)
{
if(n <= 1) return 1;
return fibonacci(n-1)+fibonacci(n-2);
}
排列问题
public class QuanPaiLie {
//再写一个类,用来进行对n个数进行全排列,简化主类。
public static void QuanPaiLie(ArrayList<Integer> listA, ArrayList<Integer> listB)i
int len_b = listB.size();
if(len_b==0) {
System. out. println(listA);
return;
}
else {
for(int i=0; i<len_b; i++) {
ArrayList<Integer> tempA = (ArrayList<Integer>) listA.clone();//克隆
ArrayList<Integer> tempB = (ArrayList<Integer>) listB.clone();
tempA. add( tempB. remove(i));//把tempB里的数加到tempA里去,i值不同,实现排列
QuanPaiLie(tempA, tempB);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System. out. println("请输入");
Scanner scanner = new Scanner(System.in);
int n = scanner . nextInt();//数组长度为n
ArrayList<Integer> listA = new ArrayList<Integer>();
ArrayList<Integer> listB = new ArrayList<Integer>();
int Count = 1;//记录一共有多少种排列
//初始化待排列的ListB
for (int i = 1; i < n; i++) {
listB.add(i);
Count *= i;//阶乘
}
System.out.println("\n 总共有"+Count + " 种排列组合:");
//对listB进行全排列
QuanPaiLie(listA, listB);
}
}
边栏推荐
- 软件测试面试题:你们的性能测试需求哪里来?
- Huawei's Hubble investment shares in VCSEL chip manufacturer Zonghui Xinguang
- 普通设备能不能接入TSN时间敏感网络?
- Thoroughly understand kubernetes scheduling framework and plug-ins
- 面试题 01.09. 字符串轮转
- My rich second generation friend
- [C language] file operation
- Briefly understand namenode and datanode
- 我的富二代朋友
- 字节月薪28K,分享一波我的自动化测试经验....
猜你喜欢

在一个字符串里面统计给定字符串的个数

Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"

普通设备能不能接入TSN时间敏感网络?

Shutter -- password login registration interface

Leetcode 2351. the first letter that appears twice
![[game] Nintendo Nintendo switch ultra detailed purchase / use guide and precautions (continuous update according to your own use...)](/img/7e/9e0e17e2ea8b8679ad7e1750a8b6d1.png)
[game] Nintendo Nintendo switch ultra detailed purchase / use guide and precautions (continuous update according to your own use...)

现货白银如何计算盈亏

伦敦银开盘时间知多少

HarmonyOS 3正式发布:鸿蒙手机流畅安全,鸿蒙终端常用常新

Codeforces暑期训练周报(7.21~7.27)
随机推荐
迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码
Flutter--密码登录注册界面
Three instance
QT setting Icon
自定义事件
Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used
Principle of logistic regression
Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24
Day 013 one dimensional array exercise
牛客多校第三场A,C+权值线段树
Insider of container network hard core technology (7) sailing on the sea depends on the helmsman
Let's move forward together, the 10th anniversary of Google play!
qt 设置图标
URDF 集成 Gazebo
Introduction and configuration of vsftpd
同心向前,Google Play 十周年啦!
软件测试面试题:如何发现数据库的相关问题?
Oxygen temperature and humidity module
Lua advanced
Software test interview question: think_ What is the function of time?