1.斐波那契数列 Fibonacci class fab{ public static void main(String args[]){ // fab(47) int 溢出 for(int i=0;i<47;i++) System.out.print(fab(i)+" "); System.out.println(); } public static int fab(int n){ if(n==0) return 0; if(n==1) return 1; int fa=0; int fb=1; for(int i=1;i<=n/2;i++){ fa+=fb; fb+=fa; } return n%2==0?fa:fb; } } 2.合并两个有序数组 public static int[] combine(int arra[],int arrb[]){ int lengtha=arra.length,lengthb=arrb.length; int result[]=new int[lengtha+lengthb]; int i=0,j=0,k=0; while(i<lengtha){ while(j<lengthb){ if(arra[i]<arrb[j]){ result[k++]=arra[i++]; }else{ result[k++]=arrb[j++]; } break; } if(j==lengthb) break; } while(i<lengtha) result[k++]=arra[i++]; while(j<lengthb) result[k++]=arrb[j++]; return result; }
时间: 2024-10-16 14:15:28