Java 第(N-2)次和第(N-1)次的第N次输出和
不管我怎么想,我都想不出这个问题,我可能只是想得太多了,我能得到一点帮助吗?我已经尝试了各种不同的for循环,我可以;我不知道如何得到这些值Java 第(N-2)次和第(N-1)次的第N次输出和,java,for-loop,while-loop,Java,For Loop,While Loop,不管我怎么想,我都想不出这个问题,我可能只是想得太多了,我能得到一点帮助吗?我已经尝试了各种不同的for循环,我可以;我不知道如何得到这些值 for(int a = 1; a<1000; a++) for(int a=1;a您可以使用递归来执行此操作: public int fibonacci(int n) { if(n == 0) { return 0; } else if(n == 1) { return 1; } else
for(int a = 1; a<1000; a++)
for(int a=1;a您可以使用递归来执行此操作:
public int fibonacci(int n) {
if(n == 0) {
return 0;
} else if(n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
您可以使用递归来执行此操作:
public int fibonacci(int n) {
if(n == 0) {
return 0;
} else if(n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
如果要使用循环,可以执行以下操作(从answer中借用):
public int fibonacci(int n){
如果(n==0)
返回1;
如果(n==1)
返回3;
int祖父母=1;
int父代=3;
int curr=0;
对于(int i=2;i如果您想使用循环,可以执行以下操作(从答案中借用):
public int fibonacci(int n){
如果(n==0)
返回1;
如果(n==1)
返回3;
int祖父母=1;
int父代=3;
int curr=0;
对于(inti=2;i,这里是不使用递归的方法
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);
}
}
}
下面是不使用递归的方法
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);
}
}
}
尝试:
inta=1,b=1;
对于(b=1;b<10000;b+=a){
系统输出打印(a+“”+b+“”);
a+=b;
}
试试:
inta=1,b=1;
对于(b=1;b<10000;b+=a){
系统输出打印(a+“”+b+“”);
a+=b;
}
将while循环转换为for循环,将是:
for (int a=1, int b=1; a < 10000; b+=a+=b ){
System.out.print(a+" "+b+" ");
}
for(inta=1,intb=1;a<10000;b+=a+=b){
系统输出打印(a+“”+b+“”);
}
将while循环转换为for循环,将是:
for (int a=1, int b=1; a < 10000; b+=a+=b ){
System.out.print(a+" "+b+" ");
}
for(inta=1,intb=1;a<10000;b+=a+=b){
系统输出打印(a+“”+b+“”);
}
有关如何执行此操作的信息,请参阅,您可以使用Binet的公式(复制如下)在不递归的情况下执行此操作:
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);
}
}
…成为以下for循环:
for (int var = 0; i != n; i++) {
...
}
有关如何执行此操作的信息,请参阅,您可以使用Binet的公式(复制如下)在不递归的情况下执行此操作:
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);
}
}
…成为以下for循环:
for (int var = 0; i != n; i++) {
...
}
我只是想说,你是如何在shell中实现这一点的:
red@cricket:~$ ./fib.sh
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
red@cricket:~$ 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
answer!或者一个递归的bash
解决方案。在这里,你怎么能在shell中实现呢
red@cricket:~$ ./fib.sh
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
red@cricket:~$ 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
answer!或者一个递归的bash
解决方案。问题是什么?问题是什么?唯一的问题是我应该用一个for循环来做这件事,这是我无法理解的。嗯,那会很慢。递归并不总是解决问题的最佳方法;-)唯一的问题是,我应该用for循环来做这件事,这是我无法理解的。嗯,那会非常慢。递归并不总是解决问题的最佳方法;-)也许删除TODO?也许删除TODO?这花了我很多时间才得到正确的答案:)。这花了我很多时间才得到正确的答案:)。