运用递归实现阶乘算法实现求自然数的阶乘

糗百君的飞船出了一点小毛病
糗百君的飞船出了一点小毛病……
莫慌, 点击 可以找到出路江山代有才人出,各领风骚数百年
用递归方法求n的阶乘(C语言)
用递归方法求n!
#include&stdio.h&
int main()
int fac(int n);
printf("input a integer number:");
scanf("%d",&n);
printf("%d!=%d\n",n,y);
int fac(int n)
printf("n&0,data error!");
else if(n==0||n==1)
f=fac(n-1)*n;
return(f);
运行结果:
input an integer number:10
10!=3628800
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!java递归求解1-5阶乘之和
[问题点数:40分,结帖人xfsunxiaolong]
本版专家分:18
结帖率 96.3%
CSDN今日推荐
本版专家分:910
本版专家分:4418
本版专家分:13063
本版专家分:221
本版专家分:1260
本版专家分:77
本版专家分:77
本版专家分:3651
本版专家分:34
本版专家分:710
结帖率 70.59%
本版专家分:710
结帖率 70.59%
匿名用户不能发表回复!|
CSDN今日推荐用java程序写一个用递归和非递归方法求n的阶乘_百度知道
该问题可能描述不清,建议你
用java程序写一个用递归和非递归方法求n的阶乘
我有更好的答案
public int factorial(int m)
if (m & 0)
else if ( m == 1)
reteurn 1;
else if (m & 1)
return m * factorial(m-1);
public int factorial(int m){
if (m & 0)
else if ( m == 1)
reteurn 1;
else if (m & 1){
int sum = 1
for (int i = 2; i &= i++)
sum = sum *}}
采纳率:52%
public class Fac { public static void main(String[] args) {
int n = 5;
long fac = digui(n);
long fac2 = nonDigui(n); }//循环方法 private static long nonDigui(int n) {
long fac = 1L;
for(int i = 1; i&=i++){
fac = fac *
}//递归 private static long digui(int n) {
if(n == 1){
return n * digui(n-1);
谢谢啊!方便留一下你的QQ号码,好吗?或者微博名称,我以后有问题私信你。谢谢了。
为您推荐:
其他类似问题
阶乘的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。博客分类:
Java中求阶乘的算法
1.一般算法:
public class Factorial {
public static int factorial(int n) {
if (n & 0 || n & 16) {
System.err.println("n must be great than 0 and less than 17");
return -1;
} else if (n == 0) {
int result = 1;
for (int i = 1; i &= i++) {
public static void main(String args[]) {
System.out.println("result = " + factorial(5));
运行结果:result = 120
2.递归算法:
public class Factorial {
public static int recursion(int n) {
if (n & 0 || n & 16) {
System.err.println("n must be great than 0 and less than 17");
return -1;
} else if (n == 0) {
return n * recursion(n - 1);
public static void main(String[] args) {
System.out.println("result = " + recursion(16));
运行结果:result =
3.使用BigInteger
import java.math.BigI
public class Factorial {
public static BigInteger bigInteger(int n) {
BigInteger result = new BigInteger("1");
if (n & 0) {
System.err.println("n must be great than 0");
return new BigInteger("-1");
} else if (n == 0) {
return new BigInteger("1");
for (int i = 1; i &= i++) {
BigInteger num = new BigInteger(String.valueOf(i));
result = result.multiply(num);
public static void main(String[] args) {
System.out.println("result = " + bigInteger(100));
运行结果:result = 63895
第二种求n的阶乘好像不对啊,但是我找不到毛病出在哪了,12的阶乘能对,到了13就出错了,13的阶乘是6 227 020 800,而用这个程序则是,还望指教
好像是超出int型的最大范围(65536)了
浏览: 64815 次
来自: 福州
dreampower 写道itcast
chuanzhi b ...
chuanzhi boke,啊哈哈
郭鹏恩 写道第二种求n的阶乘好像不对啊,但是我找不到毛病出在哪 ...
第二种求n的阶乘好像不对啊,但是我找不到毛病出在哪了,12的阶 ...
介绍这三种方法有什么目的,是让我们知道Spring容器如何做的 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 递归求阶乘和 的文章

 

随机推荐