public static void feibolaqi(){
int n1=1;
int n2=1;
int outn=0;
for(int i=0;i<5;i++){
outn=n2;
n2+=n1;
n1=outn;
System.out.println(outn);
}
}
public static void maopao(){
int [] ns = new int[]{4,2,5,8,7,9,1,3,2};
for (int i = 0; i <ns.length ; i++) {
for (int j = 0; j < i; j++) {
if (ns[i] > ns[j]) {
int t=ns[i];
ns[i]=ns[j];
ns[j]=t;
}
}
}
System.out.println(Arrays.toString(ns));
}
沒覺得相似,一個一層for循環,一個兩層for循環。冒泡的中間那3句是交換變量,斐波那契數列中間那三句是把數組向後推。