这是我的源代码:
public class Fibbonaci {
static int count;
static int size;
public static int Fibbonaci(int n) {
count++;
if (n == 0) {
return 1;
}
if (n % 10 == 0) {
return (Fibbonaci(n / 2) + Fibbonaci(n / 5));
} else if (n % 6 == 0) {
return (Fibbonaci(n / 2) + Fibbonaci(n / 3));
} else {
return (Fibbonaci(n / 3) + Fibbonaci(n / 5));
}
}
public static void main(String[] args) {
int[] lim = new int[1024];
for (int n = 1; n <= lim.length - 1; n++) {
count = 0;
Fibbonaci(n);
size = (int) Math.floor(Math.log(n) / Math.log(2)) + 1;
System.out.print("T(" + size + ") = ");
System.out.println("" + count);
}
}}
我目前的一些输出如下:
T(1) = 3
T(2) = 3
T(2) = 5
T(3) = 5
T(3) = 7
T(3) = 9
T(3) = 7
T(4) = 7
T(4) = 9
T(4) = 11
T(4) = 9
T(4) = 15如何打印大小的最大值,并计算出类似于T(1) =3T(2)=5T(3)=9T(4)= 15的输出?
发布于 2014-02-18 20:14:50
存储每个大小的值列表(最好是在一个Map中),对这个列表进行排序并打印最大的元素。
编辑甚至更简单--覆盖Map<Integer, Integer>中每个大小的值,并且由于最新的结果是最大的,Map中的最后一个值是这个大小的最大值。
发布于 2014-02-18 20:19:02
您可以将最大值存储在变量中,并在循环中进行比较。
maxValue = 0;
if ( X > maxValue ){
maxValue = X
}此外,您还可以使用集合并对其进行排序,使用比较器。但是你要知道,Fibonacci有更好的实现(只是google的实现)
使用列表订购:how to get maximum value from the List/ArrayList
使用比较器http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
https://stackoverflow.com/questions/21864288
复制相似问题