【ACM】HUD 1021 Fibonacci Again (for java)
阿新 • • 發佈:2019-01-13
規律題
簡單的使用long資料型別會在第87項溢位
通過對各項除3取餘發現每8項是一個迴圈
so。。。。
import java.util.Scanner; public class Main { public static int fib(int n) { n = n % 8; if (n == 0) return 7; if (n == 1) return 11; else { int[] lala = new int[n+1]; lala[0] = 7; lala[1] = 11; for (int i = 2; i <= n; i++) { lala[i] = lala[i-1] + lala[i-2]; } return lala[n]; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int k = scanner.nextInt(); int result = fib(k) % 3; if (result == 0) System.out.println("yes"); else System.out.println("no"); } } }