You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
6 years ago
|
package com.fr.plugin.function.fib;
|
||
|
|
||
|
import com.fr.general.GeneralUtils;
|
||
|
import com.fr.intelli.record.Focus;
|
||
|
import com.fr.intelli.record.Original;
|
||
|
import com.fr.record.analyzer.EnableMetrics;
|
||
|
import com.fr.script.AbstractFunction;
|
||
|
import com.fr.stable.ArrayUtils;
|
||
|
import com.fr.stable.Primitive;
|
||
|
|
||
|
@EnableMetrics
|
||
|
public class Fibonacci extends AbstractFunction {
|
||
|
|
||
|
@Override
|
||
|
@Focus(id = "com.fr.plugin.function.fib", text = "", source = Original.PLUGIN)
|
||
|
public Object run(Object[] args) {
|
||
|
if (ArrayUtils.isEmpty(args)) {
|
||
|
return Primitive.ERROR_VALUE;
|
||
|
}
|
||
|
int n = GeneralUtils.objectToNumber(args[0]).intValue();
|
||
|
if (n < 0) {
|
||
|
return Primitive.ERROR_VALUE;
|
||
|
}
|
||
|
return getFib(n);
|
||
|
}
|
||
|
|
||
|
private int getFib(int n) {
|
||
|
if (n < 0) {
|
||
|
return -1;
|
||
|
} else if (n == 0) {
|
||
|
return 0;
|
||
|
} else if (n == 1 || n == 2) {
|
||
|
return 1;
|
||
|
} else {
|
||
|
int[] fibAry = new int[n + 1];
|
||
|
fibAry[0] = 0;
|
||
|
fibAry[1] = fibAry[2] = 1;
|
||
|
for (int i = 3; i <= n; i++) {
|
||
|
fibAry[i] = fibAry[i - 1] + fibAry[i - 2];
|
||
|
}
|
||
|
return fibAry[n];
|
||
|
}
|
||
|
}
|
||
|
}
|