java 列出斐波那契数列的前n项
2009-11-12 20:59:51 来源:WEB开发网核心提示:view plaincopy to clipboardPRint?public class Fibonacci { /** * @param args */public static void main(String[] args) { // TODO Auto-generated method stub NumOf
view plaincopy to clipboardPRint?
public class Fibonacci {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NumOfFibonacci(9);
}
public static int MyFibonacci(int i){
if(i>0) {
if(i == 1)return 1;
if(i == 2)return 1;
else return MyFibonacci(i-1)+MyFibonacci(i-2);
}
else
return 0;
}
//获得数列的前n项
public static void NumOfFibonacci(int n){
String s = "斐波那契数列的前"+n+"项:";
for(int i=1; i<=n; i++){
s += MyFibonacci(i)+" ";
}
System.out.println(s);
}
//result
//斐波那契数列的前9项:1 1 2 3 5 8 13 21 34
}
public class Fibonacci {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NumOfFibonacci(9);
}
public static int MyFibonacci(int i){
if(i>0) {
if(i == 1)return 1;
if(i == 2)return 1;
else return MyFibonacci(i-1)+MyFibonacci(i-2);
}
else
return 0;
}
//获得数列的前n项
public static void NumOfFibonacci(int n){
String s = "斐波那契数列的前"+n+"项:";
for(int i=1; i<=n; i++){
s += MyFibonacci(i)+" ";
}
System.out.println(s);
}
//result
//斐波那契数列的前9项:1 1 2 3 5 8 13 21 34
}
更多精彩
赞助商链接