2014-02-28 211 views
1

無論我嘗試什麼,我都無法想出這個問題,我可能只是在想它,我能得到一點幫助嗎?香港專業教育學院已經嘗試過各種不同的for循環,我可以;噸弄清楚如何獲取這些值第(N-2)和第(N-1)的第N個輸出總和

for(int a = 1; a<1000; a++) 

我想使它所以它規定A + B,但我不認爲那可能

下面的代碼以Fibonacci着名,其中第N個輸出是(N- 2)和第(N-1)個值的和。編寫for-expression來完成相同的任務。

int a=1, b=1; 
while (a < 10000){ 
System.out.print(a+" "+b+" "); 
b+=a+=b; 
} 
+1

這是什麼問題? – 2014-02-28 06:20:49

回答

2

轉換while循環到一個for循環,這將是:

for (int a=1, int b=1; a < 10000; b+=a+=b){ 
    System.out.print(a+" "+b+" "); 
} 
+0

這讓我很多答案得到正確的一個:)。 – PlasmaPower

1

您可以使用遞歸來做到這一點:

public int fibonacci(int n) { 
    if(n == 0) { 
     return 0; 
    } else if(n == 1) { 
     return 1; 
    } else { 
     return fibonacci(n - 1) + fibonacci(n - 2); 
    } 
} 
+0

唯一的一點是,我應該做一個for循環,這是我無法弄清楚的 – user3363245

+0

呃,那真的很慢。遞歸併不總是解決問題的最好方法;-) – Henry

+0

遞歸很酷:) –

1

如果你想使用一個循環,你可以做以下的(從this答案借來的):

public int fibonacci(int n) { 
    if (n == 0) 
     return 1; 
    if (n == 1) 
     return 3; 
    int grandparent = 1; 
    int parent = 3; 
    int curr = 0; 
    for(int i=2; i <= n; i++){ 
     curr = 3 * parent - grandparent; 
     grandparent = parent; 
     parent = curr; 
    } 
    return curr; 
} 
0

這裏的你如何做,而不遞歸。

package fib; 

    public class fib { 

      public static void main(String[] args) { 
        int first = 1; 
        int second = 1; 
        int thrid = 2; 
        System.out.format("0, %d, %d, %d", first, second, thrid); 
        while (thrid < 1000) { 
          first = second; 
          second = thrid; 
          thrid = first + second; 
          System.out.format(", %d", thrid); 
        } 
      } 
    } 

輸出:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597 
+0

也許刪除TODO? – PlasmaPower

1

嘗試:

int a=1, b=1; 
for (b=1; b < 10000; b+=a){ 
System.out.print(a+" "+b+" "); 
a+=b; 
} 
0

爲怎麼看algorithm 3做到這一點,你可以不用遞歸使用比奈的公式,下面複製:

import java.lang.Math; 
import java.math.BigInteger; 
import java.math.BigDecimal; 
import java.math.MathContext; 
import java.math.RoundingMode; 
class f3c { 
// This program calculates the nth fibonacci number 
// using alrogirhtm 3C: Binet's formula with rounding 
// 
// compiled: javac f3c.java 
// executed: java f3c n 
// 

    // Method f3c.isqrt(n) finds the inverse root of n using the following 
    // recurrent equation: y(n+1) = 0.5*y(n)*[3 - x*y(n)^2] 
    // It is faster than typical Newton-Raphson square root finding because 
    // it does not use division. 
    // Funny how Java implemented exponentiation by repeated squaring 
    // for BigDecimals, but did not implement any sort of square root. 
private static BigDecimal isqrt(BigDecimal x, MathContext mc) { 
    BigDecimal guess = new BigDecimal(1/Math.sqrt(x.doubleValue())); 
    BigDecimal three = new BigDecimal(3); 
    BigDecimal half = new BigDecimal(0.5); 
    BigDecimal oldguess; 
    do{ oldguess = guess; 
     guess = half.multiply(guess.multiply(
        three.subtract(x.multiply(guess.pow(2)))), mc); 
    } while (oldguess.compareTo(guess) != 0); 

    return guess; 
} 

// method f3c.fib(n) calculates the nth fibonacci number 
// as floor(phi^n/sqrt(5) + 1/2), where phi = (1+sqrt(5.0))/2; 
private static BigInteger fib(int n) { 
    MathContext mc = new MathContext(n/2, RoundingMode.HALF_DOWN); 
    BigDecimal is5 = isqrt(new BigDecimal("5"), mc); 
    BigDecimal one = BigDecimal.ONE; 
    BigDecimal half = new BigDecimal(0.5); 
    BigDecimal phi = half.multiply(one.add(one.divide(is5, mc))); 
    return phi.pow(n, mc).multiply(is5).toBigInteger(); 
} 

// Method f3c.f(n) handles the negative arguments: F(-n) = F(n)*(-1)^(n+1) 
private static BigInteger f(int n) { 
    if(n<0) 
     return (n%2==0) ? fib(-n).negate() : fib(-n); 
    else 
     return fib(n); 
} 

// Method f3c.f_print(n) prints the nth Fibonacci number 
private static void fib_print(int n) { 
    System.out.println(n + "th Fibonacci number is " + f(n)); 
} 
// Method f3c.main is the program entry point 
// It makes sure the program is called with one commandline argument 
// converts it to integer and executse fib_print 
// If the conversion fails or if the number of arguments is wrong, 
// usage information is printed 
public static void main(String argv[]) { 
    try { 
     if(argv.length == 1) { 
      fib_print(Integer.parseInt(argv[0])); 
      System.exit(0); 
     } 
    } catch (NumberFormatException e) { } 
    System.out.println("Usage: java f3c <n>"); 
    System.exit(1); 
} 
} 

不過,如果你想僅僅while循環轉換爲一個for循環中,一般原則是,代碼如下所示:

int var = 0; 
while (var != n) { 
    ... 
    var++; 
} 

...成爲循環如下:

for (int var = 0; i != n; i++) { 
    ... 
} 
0

只要是傻在這裏你怎麼能做到這一點在外殼:

[email protected]:~$ ./fib.sh 
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 
[email protected]:~$ cat fib.sh 
#!/bin/sh 

first=0 
second=1 
echo -n "$first, $second, " 
thrid=`expr $first + $second` 
echo -n $thrid 
while [ $thrid -lt 100 ] 
do 
     first=$second 
     second=$thrid 
     thrid=`expr $first + $second` 
     echo -n ", $thrid" 
done 
echo "" 

哈哈!有些需要添加python答案!或者遞歸的bash解決方案。