①、斐波那契数列
1 static void method(){ 2 3 String a = "1",b="1"; 4 char[] aArr; 5 char[] bArr; 6 int num = 0; 7 int i,j; 8 int temp = 0; 9 int k; 10 System.out.print(" "+a+" "+b); 11 for(i =2 ; i<=500;i++){ 12 aArr = a.toCharArray(); 13 bArr = b.toCharArray(); 14 a = b; 15 k = bArr.length - aArr.length; 16 for(j = aArr.length-1;j>=0;j--){ 17 temp = (bArr[j+k]-'0')+(aArr[j]-'0') + num; 18 num = 0; 19 if(temp >=10){ 20 temp -=10; 21 num = 1; 22 } 23 bArr[j+k] = (char) (temp + '0'); 24 } 25 while(j+k>=0 &&num==1){ 26 temp = (bArr[j+k] - '0') + num; 27 if(temp > 10){ 28 bArr[j+k] = (char) (bArr[j+k] + (temp - 10)); 29 num = 1; 30 }else{ 31 num = 0; 32 } 33 j--; 34 } 35 36 if(num ==1){ 37 char[] cArr = new char[bArr.length+1]; 38 for(int l=bArr.length-1;l>=0;l--){ 39 cArr[l+1] = bArr[l]; 40 } 41 cArr[0] = 1 + '0'; 42 b = String.valueOf(cArr); 43 }else{ 44 b =String.valueOf(bArr); 45 } 46 if(i%10 ==0) 47 System.out.println(); 48 System.out.print(" "+b); 49 } 50 51 }
②、找大数组中的最大的N位
1 /* 2 * a为源数组,b为要找最大的多少位 3 */ 4 static int[] method(int a[], int b){ 5 6 int[] nums = new int[b]; 7 8 for(int i = 0;i<b;i++) 9 nums[i] = a[i]; 10 11 Arrays.sort(nums); 12 13 for(int i = b;i<a.length;i++){ 14 15 if(a[i]>nums[b-1]){ 16 int j = b-1; 17 while(j>0&&a[i]>nums[j]){ 18 nums[j] = nums[j-1]; 19 j--; 20 } 21 22 nums[j] = a[i]; 23 24 } 25 } 26 27 return nums; 28 29 }