⑴ 用java递归算法求一个数字的阶乘
用递归算法求来一个数字源的阶乘的程序如下:
public class JieCheng {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个整数:");
int n = in.nextInt();
System.out.println(n+"!="+f(n));
}
static long f(int n){
if (n==1) return 1;
else return n*f(n-1);
}
}
运行结果:
请输入一个整数:6
6!=720
⑵ java用函数调用求阶乘
public class Factorial{
public Factorial(){};
public Factorial(int i){
System.out.println(recursion(i));//递归方法求
}
private int recursion(int i){ ----------递归方法主体
if(i<0) //<0退出
return -1;
else if(i==0) //0的阶乘=1
return 1;
else //0继续递归
return i*recursion(i-1);
}
public static void main(String[] args){
Factorial factorial=new Factorial(5); //5的阶乘
}
}
////////////以下是用你的程序改的,主要是格式问题,思想是对的。你这个//////////是循环求阶乘,我上面那个是递归求阶乘
public class 阶乘 {
public static long Jiecheng(int x){
long y=1;
for(int i=1;i<=x;i++)
y=y*i;
return y;
}
public static void main(String args[])
{
System.out.println(阶乘.Jiecheng(5));
}
}
⑶ 在java中,用递归方法计算n的阶乘怎么输入
用Java求键盘输入的数的阶乘n。(递归算法)packagejiecheng; importjava.util.*; //导入java.util包中的所有类classrep{ publiclongrep(intn){ longi=0; if(n==0||n==1) i=1;
elsi=n*rep(n-1) returni; } } publicclassJie{ publicstaticvoidmain(String[]args){ intn; //此处定义要输入的数Scanners= newScanner(System.in); //以下三行用于n的值得输入System.out.print( "请输入一个整数:"); n=s.nextInt(); repf= newrep(); System.out.println(n+"!="+f.rep(n)); } }
⑷ 用java编写计算N的阶乘
int njiecheng(int x)
{
if(i>1)
{
x=x*njiecheng(x-1);
}
else return 1;
}
⑸ java递归算法的例子。
阶乘:
要求:给定一个数值,计算出它的阶乘值,例如5的阶乘为5*4*3*2*1
实现:
[html] view plain
<span style="font-size:12px;"> // 利用递归实现一个数的阶乘值 private static BigDecimal getNum(BigDecimal inNum) { if (inNum.compareTo(BigDecimal.ONE) == 0) { return inNum; } return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE))); }</span>
(2)Fibonacci数列:1,1,2,3,5,8,13……
要求:找出数列中指定index位置的数值
实现:
[html] view plain
<span style="font-size:12px;"> // 利用递归实现了Fibonacci数列 private static int fab(int index) { if (index == 1 || index == 2) { return 1; } else { return fab(index - 1) + fab(index - 2); } }</span>
(3)汉诺塔
要求:汉诺塔挪动
实现:
[html] view plain
<span style="font-size:12px;"> <span style="white-space:pre;"> </span>private static final String DISK_B = "diskB"; <span style="white-space:pre;"> </span>private static final String DISK_C = "diskC"; <span style="white-space:pre;"> </span>private static final String DISK_A = "diskA"; <span style="white-space:pre;"> </span>static String from=DISK_A; <span style="white-space:pre;"> </span> static String to=DISK_C; <span style="white-space:pre;"> </span> static String mid=DISK_B; <span style="white-space:pre;"> </span> public static void main(String[] args) { <span style="white-space:pre;"> </span> String input=JOptionPane.showInputDialog("please input the number of the disks you want me move."); <span style="white-space:pre;"> </span> int num=Integer.parseInt(input); <span style="white-space:pre;"> </span> move(num,from,mid,to); <span style="white-space:pre;"> </span> }</span>
[html] view plain
<span style="font-size:12px;"> // 利用递归实现汉诺塔 private static void move(int num, String from2, String mid2, String to2) { if (num == 1) { System.out.println("move disk 1 from " + from2 + " to " + to2); } else { move(num - 1, from2, to2, mid2); System.out.println("move disk " + num + " from " + from2 + " to " + to2); move(num - 1, mid2, from2, to2); } }</span>
(4)排列组合
要求:将输入的一个字符串中的所有元素进行排序并输出,例如:你给出的参数是"abc",
则程序会输出
abc
acb
bac
bca
cab
cba
实现:
[html] view plain
<span style="font-size:12px;"><span style="white-space:pre;"> </span>public static void permute(String str) { <span style="white-space:pre;"> </span> char[] strArray = str.toCharArray(); <span style="white-space:pre;"> </span> permute(strArray, 0, strArray.length - 1); <span style="white-space:pre;"> </span>}</span>
[html] view plain
<span style="font-size:12px;"> // 利用递归实现,将输入的一个字符串中的所有元素进行排序并输出 public static void permute(char[] list, int low, int high) { int i; if (low == high) { String cout = ""; for (i = 0; i <= high; i++) { cout += list[i]; } System.out.println(cout); } else { for (i = low; i <= high; i++) { char temp = list[low]; list[low] = list[i]; list[i] = temp; permute(list, low + 1, high); temp = list[low];
⑹ java递归求数字10的阶乘。
我按照我的理解给你解答一下,希望你能看明白。
我理解中的递归,就是在没完成当前运算的情况下,先完成当前运算的上一级,如上一级仍未完成则继续推向上一级,知道完成某一级运算后,在逐条往回返,最终的运算结果就是最终的值。
i 为 10时,add(10) = 10*(add(9)),此时10为整数,而我们需要计算出add(9)为多少;
i 为 9 时,add(9) = 9*(add(8)),这样再结合上一级 add(10) = 10*9*(add(8))
...
i 为2时, add(2) = 2*(add(1))=2*1,因此add(10) = 10*9*8*7*6*5*4*3*2*1.
⑺ 使用java程序接收一个小于10的整数n,计算并输出阶乘.
我的注释蛮清楚的,希望对你有助!
import java.util.Random; //引入包.
class TestR
{
public static void main(String[] args)
{
Random r=new Random(); //创建Random 对象r.
int n=r.nextInt(10); //产生小于10 的整数给 n保存.
System.out.println("n="+n); //打印版随机产生的整数 n.
System.out.println("n 的阶权乘= "+recursion(n)); //打印阶乘.
}
public static int recursion(int n) //定义函数递归,以求阶乘.
{
if (n==1 || n==0)
{
return 1;
}else
return n*recursion(n-1);
}
}