Fibonacci Again
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 97537 Accepted Submission(s): 43162
Problem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).OutputPrint the word "yes" if 3 divide evenly into F(n).
Print the word "no" if not.Sample Input012345Sample Outputnonoyesnonono
这道题就是看f(n)能不能被3整除,一开始用了递归算法,超时了,无奈,找规律吧。能得出在2,6,10,14,18。。。时f(n)能够被3整除,就易推出,(n-2)%4==0的时候就会输出yes。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()) {
int n=scanner.nextInt();
if((n-2)%4==0) System.out.println("yes");
else System.out.println("no");
}
scanner.close();
}
}