这是我在简书上写的题解 https://www.jianshu.com/p/a8f157e4879a 详细解释了斐波那契算法 牛客的题解编辑器太不友好了
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
System.out.println(fib(in.nextLine()));
}
}
static String fib(String expression) {
String[] ops = expression.split("/");
long a = Long.parseLong(ops[0]);
long b = Long.parseLong(ops[1]);
StringBuilder sb = new StringBuilder();
while (true) {
long q = b / a;
long r = b % a;
if (a == 1) {
return sb.append("1/").append(b).toString();
} else if (r == 0){
return sb.append("1/").append(q).toString();
} else {
sb.append("1/").append(q + 1).append("+").toString();
}
a -= r;
b *= q + 1;
}
}
}

京公网安备 11010502036488号