1. 程式人生 > >java語言程式設計 第十版(基礎篇)7.11-16

java語言程式設計 第十版(基礎篇)7.11-16

7.11
public static double deviation(double[] x) {
		
		double standardDeviation =0;
		double numerator=0;
		double mean=0;
		
		mean=mean(x);
		for(int i =0;i<10;i++) {
			
			numerator+=Math.pow(x[i]-mean,2);
			
		}
		 standardDeviation = Math.pow(numerator/9, 0.5);
		
		return standardDeviation;
		
	}
	public static double mean(double []x) {
		double mean=0;
		double sum=0;
		for(int i =0;i<10;i++) {
			sum+=x[i];
		}
		
		mean = sum/10.0;
		return mean;
	}
	
	
	7.12
	
	public static void main(String []args) {
		double deviation=0;
		double mean=0;
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the numbers: ");
		double []array = new double[10];
		for(int i = 0;i<10;i++) {
			array[i]= input.nextDouble();
		}
		
		mean= mean(array);
		deviation= deviation(array);
		
		 
		 System.out.printf("The mean is: %4.2f",mean);
		 System.out.printf("The standard deviation: 4.2f",deviation);
		
		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int  []list = new int [10];
		System.out.print("Enter the numbers : ");
		for(int i =0;i<10;i++) {
			list[i]= input.nextInt();
		}
		
		
		list =reverse(list);//直接把原陣列覆蓋掉就行,也可在方法裡用temp實現
		for(int i =0;i<10;i++) {
			System.out.println(list[i]);
		}
		
	}
	
	public static int[] reverse(int []list) {
		int [] result = new int [list.length];
		
		for(int i =0 ,j=result.length-1;i<list.length;i++,j--) {
			
			result[j]=list[i];
		}
		
		return result;
	}

7.13

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n = getRandom(1,2,3,5);
		System.out.println(n);
	}

	public static int getRandom(int ...numbers) {
		int num = (int)(System.currentTimeMillis()%54)+1;
		if(numbers.length == 0)
			return num;
		int flag = 0;
		while(true){
			for(int i = 0; i < numbers.length; ++i){
				if(numbers[i] == num){
					flag = 1;
					break;
				}
			}
			if(flag == 1)
				num = (int)(Math.random()*54)+1;
			else
				break;
		}
		return num;
	}

7.14

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the numbers: ");
		int []num = new int [5];
		for(int i=0;i<5;i++) {
			num[i]=input.nextInt();
			
		}
		int gcd;
		gcd =gcd(num);
		System.out.println(gcd);
	}
	
	public static int gcd(int ...numbers) {
	 int num = numbers[0];
	 for(int i=1;i<numbers.length;++i) {
		 num = contrast(num,numbers[i-1]);
	 }
		return num;
	}
	
	public static int  contrast(int x1,int x2) {
		int temp =0;
		while(true) {
			temp=x1%x2;
			if(temp==0)
				break;
			x1=x2;
			x2=temp;
				
		}
		
		return x2;
	}

7.15

public static void main(String[] args) {
		// TODO Auto-generated method stub
		 Scanner input = new Scanner(System.in);
		 System.out.print("Enter the numbers ");
		 int [] num = new int [10];
		 for(int i =0;i<10;i++) {
			 num[i]=input.nextInt();
		 }
		num= eliminateDuplicates(num);
		System.out.print("The distanct numbers are ");
		 for(int i =0;i<10;i++) {
			 if(num[i]!=0)
			System.out.print(num[i]+" ");
		 }
	}
	
	public static int [] eliminateDuplicates(int[] list) {
		
		int [] num = new int [10];
		
		for(int i =0;i<10;i++) {
			for(int j=i+1;j<10;j++) {
				if(list[i]==list[j])
					list[j]=0;
			}
		}
		
		
		return list;
	}

7.16

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] array = new int [100000];
		for(int i =0;i<100000;i++) {
			array[i]=(int)(Math.random()*10000);
		}
		int num1,num2;
		long startTime1 = System.currentTimeMillis();
		num1= linearSearch(array,array[9999]);
		long endTime1 = System.currentTimeMillis();
		long executionTime1 = endTime1 -startTime1;
		System.out.println("linearSearch consume :"+executionTime1);
		
		long startTime2 = System.currentTimeMillis();
		num2= linearSearch(array,array[9999]);
		long endTime2 = System.currentTimeMillis();
		long executionTime2 = endTime2 -startTime2;
		System.out.println("binarySearch consume :"+executionTime2);
		
		

	}

	
		  /** The method for finding a key in the list */
		  public static int linearSearch(int[] list, int key) {
		    for (int i = 0; i < list.length; i++) {
		      if (key == list[i])
		        return i;
		    }
		    return -1;
		  }
		  
		  
		  public static int binarySearch(int[] list, int key) {
			    int low = 0;
			    int high = list.length - 1;

			    while (high >= low) {
			      int mid = (low + high) / 2;
			      if (key < list[mid])
			        high = mid - 1;
			      else if (key == list[mid])
			        return mid;
			      else
			        low = mid + 1;
			    }

			    return -low - 1; // Now high < low
			  }