标签:blog io java for 2014 问题 on c log
这其实是两道非常基础和简单地题。但somehow每隔一段时间我老是会不经意地想起这两个问题,有时候卡克没有一下想起解法还会急的直冒汗...................
言归正传,贴出这两题代码
(1)打印斐波拉契数列
// Java program for Fibonacci number using Loop. public static int fibonacciLoop(int number){ if(number == 1 || number == 2){ return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number }
public static void prime( int number) { for (int i=2; i<number; i++) for (int j=2; j*j<=i; j++) { if (i % j == 0) break; else if (j+1 > sqrt(i)) { System.out.println(i); } } return 0; }
标签:blog io java for 2014 问题 on c log
原文地址:http://blog.csdn.net/he_wolf/article/details/39544613