当前位置:网站首页>递归相关习题
递归相关习题
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);
}
}
边栏推荐
- Wu xiongang sent an internal letter: arm's allegations are unwarranted, and no damage is allowed to the existing achievements!
- Principle of logistic regression
- LeetCode 2347. 最好的扑克手牌
- Storage practices for high-performance computing scenarios, see here
- LeetCode 2351. 第一个出现两次的字母
- Three instance
- Login function implementation
- URDF integrated gazebo
- Codeforces暑期训练周报(7.14~7.20)
- Leetcode 2341. How many pairs can an array form
猜你喜欢

同心向前,Google Play 十周年啦!

BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (

JUC concurrent programming learning

华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘

Leetcode 2341. How many pairs can an array form

Article reproduction: super resolution network fsrcnn

迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码
![[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...)

Summary of common shortcut keys in idea

Cesium add dynamic pop-up
随机推荐
Leetcode 2347. the best poker hand
Token is used in nodejs
Software test interview question: how to find problems related to the database?
彻底搞懂kubernetes调度框架与插件
Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24
How the test architects of bat factories interpret various disputes of the test platform
PHP利用某些函数bypass waf探讨
登录功能实现
Unity Shader入门精要学习——基础纹理
Byte monthly salary 28K, share a wave of my automation testing experience
Codeforces暑期训练周报(7.21~7.27)
Advanced MySQL -- stored procedures and custom functions
docker 本地搭建mysql主从
【分布式开发】之 CAP 原则
URDF integrated gazebo
Leetcode 2351. the first letter that appears twice
BigDecimal common API
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 supplementary problem solution (a, C, J)
面试题 01.09. 字符串轮转
Count the number of given strings in a string