各种算法题MARK

①、斐波那契数列  

static void method(){
		
		String a = "1",b="1";
		char[] aArr;
		char[] bArr;
		int num = 0;
		int i,j;
		int temp = 0;
		int k;
		System.out.print(" "+a+" "+b);
		for(i =2 ; i<=500;i++){
			aArr = a.toCharArray();
			bArr = b.toCharArray();
			a = b;
			k = bArr.length - aArr.length;
			for(j = aArr.length-1;j>=0;j--){
				temp = (bArr[j+k]-'0')+(aArr[j]-'0') + num;
				num = 0;
				if(temp >=10){
					temp -=10;
					num = 1;
				}
				bArr[j+k] = (char) (temp + '0');
			}
			while(j+k>=0 &&num==1){
				temp = (bArr[j+k] - '0') + num;
				if(temp > 10){
					bArr[j+k] = (char) (bArr[j+k] + (temp - 10));
					num = 1;
				}else{
					num = 0;
				}
				j--;
			}
			
			if(num ==1){
				char[] cArr = new char[bArr.length+1];
				for(int l=bArr.length-1;l>=0;l--){
					cArr[l+1] = bArr[l];
				}
				cArr[0] = 1 + '0';
				b = String.valueOf(cArr);
			}else{
				b =String.valueOf(bArr);
			}
			if(i%10 ==0)
				System.out.println();
			System.out.print(" "+b);
		}
		
	}



②、找大数组中的最大的N位

/*
	 *	a为源数组,b为要找最大的多少位 
	 */
	static int[] method(int a[], int b){
		
		int[] nums = new int[b];
		
		for(int i = 0;i<b;i++)
			nums[i] = a[i];
		
		Arrays.sort(nums);
		
		for(int i = b;i<a.length;i++){
			
			if(a[i]>nums[b-1]){
				int j = b-1;
				while(j>0&&a[i]>nums[j]){
					nums[j] = nums[j-1];
					j--;
				}
					
				nums[j] = a[i];
				
			}
		}
		
		return nums;
		
	}



版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-03-07 16:29  KiraChen  阅读(215)  评论(0编辑  收藏  举报