我正在练习递归,我看不出为什么这个方法似乎不起作用。 任何想法?基本的递归方法 - 阶乘
public void fact()
{
fact(5);
}
public int fact(int n)
{
if(n == 1){
return 1;
}
return n * (fact(n-1));
}
}
感谢
我正在练习递归,我看不出为什么这个方法似乎不起作用。 任何想法?基本的递归方法 - 阶乘
public void fact()
{
fact(5);
}
public int fact(int n)
{
if(n == 1){
return 1;
}
return n * (fact(n-1));
}
}
感谢
你的代码似乎工作,但你没有做与返回的任何有价值的东西,把方法调用fact
或fact(5)
一个System.out.println
的内部,看看你会得到什么。
工作正常。你没有把它分配给任何东西。这是一个可以证明它有效的测试。
@Test
public void testYourFactorialMethod() {
assertEquals(120, fact(5));
}
不是'assertEquals()'一个JUnit API方法吗?我猜JUnit尚未在这里启用,所以你应该更明确一些。如果它实际上是一个常规的API方法,_mea culpa_。 – Pops 2010-05-10 13:25:48
是的 - 这是JUnit - 我试图展示的是他的方法有效。如果你阅读我的评论,我不建议他在任何地方插入我的代码。 – andyczerwonka 2010-05-10 16:42:09
用递归方法写斐波纳契是完全错误的!
这是因为如果你写Fibonatcci递归,计算120
你需要36年toget结果的好/坏Algorythm如何影响任何项目
昔日经典例子!!!!!!
public static int Fibonacci(int x)
{ // bad fibonacci recursive code
if (x <= 1)
return 1;
return Fibonacci(x - 1) + Fibonacci(x - 2);
}
在
点网4.0有一个新的类型名称BigInteger和你可以用它来制作使用系统更好的功能
; using System.Collections.Generic;使用System.Numerics的 ; //需要参考。该装配
namespace Fibonaci
{
public class CFibonacci
{
public static int Fibonacci(int x)
{
if (x <= 1)
return 1;
return Fibonacci(x - 1) + Fibonacci(x - 2);
}
public static IEnumerable<BigInteger> BigFib(Int64 toNumber)
{
BigInteger previous = 0;
BigInteger current = 1;
for (Int64 y = 1; y <= toNumber; y++)
{
var auxiliar = current;
current += previous;
previous = auxiliar;
yield return current;
}
}
}
}
,您可以使用它像
using System;
using System.Linq;
namespace Fibonaci
{
class Program
{
static void Main()
{
foreach (var i in CFibonacci.BigFib(10))
{
Console.WriteLine("{0}", i);
}
var num = 12000;
var fib = CFibonacci.BigFib(num).Last();
Console.WriteLine("fib({0})={1}", num, fib);
Console.WriteLine("Press a key...");
Console.ReadKey();
}
}
}
在这种情况下,你可以计算比第二12000
少。所以
使用递归methos并不总是一个好主意
递归部分是精细导入的代码;你只是没有使用它的return
值,它会被丢弃。这里是你的阶乘代码一个完整的Java应用程序,小幅饮誉式教育目的:
public class Factorial {
public static String fact(int n) {
if(n == 1){
return "1";
}
return n + " * " + (fact(n-1)); // what happens if you switch the order?
}
public static void main(String[] args) {
System.out.println(fact(5));
// prints "5 * 4 * 3 * 2 * 1"
}
}
+1热爵士乐。 – Pops 2010-05-10 13:30:55
你的代码的简化版本:
public int fact(int n)
{
if(n == 1){
return 1;
}
return n * (fact(n-1));
}
可能只是:
public int fact(int n)
{
return n == 1 ? 1 : n * fact(n - 1);
}
但是你的代码没有错,这只是另一种风格(如果你不习惯三元运算符保持它的方式)。我更喜欢在这些情况下使用三元运算符(注意代码是无副作用的)。
算法没有错,就像7小时前其他3个海报所说的那样。 OP只是没有做任何程序输出。 – 2010-05-10 21:04:06
但他的代码风格令人困惑。两个出口点,一个不明确的别名和不必要的括号。这是我的观点。问题不仅仅是回答。 – 2010-05-11 10:20:08
这两个退货没有错。此外,初学者可能会难以理解条件运算符的使用。 – helpermethod 2010-05-12 23:10:31
public class Recursive {
public static void main(String[] argss) {
System.out.print(fac(3));
}
public static int fac(int n) {
int value = 0;
if (n == 0) {
value = 1;
} else {
value = n * fac(n - 1);
}
return value;
}
}
// out put 6
static int factorial(int x) {
int result;
if (x == 1) {
return 1;
}
// Call the same method with argument x-1
result = factorial(x – 1) * x;
return result;
}
对于完整的例子检查这个
http://answersz.com/factorial-program-in-java-using-recursion/
尝试是这样的: (或者,也许试试这个直接)
public class factorial {
private static int factorial(int n){
if (n > 1) {
return n * (factorial(n-1));
} else {
return 1;
}
}
public static void main(String[] args) {
System.out.println(factorial(100));
}
}
究竟是什么意思“不工作”?它做什么,你期望它做什么? – 2010-05-10 13:08:13