JAVA语言程序设计(基础篇) 第十版——第七章 一维数组 (参考答案)

(7.2~7.5节)+(7.6~7.8节)+7.9节+(7.10~7.12节)

我只做到了21题,有点乏了,未完待续.........(以后更新)

(7.2~7.5节)

*7.1(指定等级)

import java.util.Scanner;

public class S1 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the number of students: ");
		int numbers=input.nextInt();
		System.out.print("Enter "+numbers+" scores: ");
		
		int[] scores=new int[numbers];
		for(int i=0; i<scores.length; i++) {
			scores[i]=input.nextInt();
		}
		
		int best=scores[0];
		for(int i=1; i<scores.length; i++) {
			if(best<scores[i])
				best=scores[i];
		}
		
		char[] ch= {'A','B','C','D','E','F'};
		
		for(int i=0; i<scores.length; i++) {
			if(scores[i]>=best-10)
				System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[0]);  
			else if(scores[i]>=best-20)
				System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[1]);  
			else if(scores[i]>=best-30)
				System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[2]); 
			else if(scores[i]>=best-40)
				System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[3]);  
			else
				System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[4]);  
		}
		
		input.close();
	}	
}

7.2(倒置输入的数)

import java.util.Scanner;

public class S2 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入10个整数: ");
		
		int[] numbers=new int[10];
		for(int i=0; i<numbers.length; i++) {
			numbers[i]=input.nextInt();
		}
		
		for(int i=numbers.length-1; i>=0; i--) {
			System.out.print(" "+numbers[i]);
		}
		
		
		input.close();
	}

}

**7.3(计算数字出现的次数)

参考大神博客会有不同解法:

https://blog.csdn.net/weixin_46215617/article/details/105097958?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://blog.csdn.net/LEE_FIGHTING_JINGYU/article/details/80009407?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

在这里我用count[1]存储数字1的次数,用count[2]存储数字2的次数,......,以此类推。

import java.util.Scanner;

public class S3 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter tne integers between 1 and 100: ");
		
		int number=input.nextInt();
		int[] counts=new int[102];
		int i=1;
		
		while(number!=0) {
			if(number>=1 && number<=100) {
				counts[number]++;
			}
			
			number=input.nextInt();
		}
		
		
		
		for(int j=1; j<=100; j++) {
			if(counts[j]>0) {
				System.out.println(j+" occurs "+counts[j]+(counts[j]>1 ? " times." :" time."));
			}
			
				
		}
		input.close();
	}

}

 

7.4(分析成绩)

某大神参考代码:

https://blog.csdn.net/lee_fighting_jingyu/article/details/80009407

我改变了一下,数组长度我让用户自己输入,以确定存储分数的数组长度。

import java.util.Scanner;

public class S4 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入你将要输入几个考试分数: ");
		int n=input.nextInt();
		
	    System.out.print("请输入考试分数(以负数结束): ");
	    int[] scores = new int[n];
	    int i=0;
	    int count=0;
	    int sum=0;
	    int number=input.nextInt();
	    
	     while(number>=0) {
	    	 if(number>=0 && number<=100) {
	    		 count++;
	    		 scores[i]=number;
	    		 
	    		 sum += scores[i];
	    		 i++;
	    	 }
	    	 else {
	    		 System.out.println("Invalid input!");
	    	 }
	    	 
	    	 number=input.nextInt();
	     }
	     
	     double average= sum / count;
	     
	     input.close();
	     
	     int low=0;
	     int height=0;
	     
	     for(int j=0; j<scores.length; j++) {
	    		  if(scores[j]>=average) {
	    		 height++;
	    		  }
	    		  else {
	    			  low++;
	    		  }
	    		  
	     }
	     
	     System.out.println("平均数是: "+average);
	     System.out.println("大于平均数的分数有: "+height);
	     System.out.println("小于平均数的分数有: "+low);

	}	
 
}

 

**7.5(打印不同的数)

我不会,我参考了一下大神的答案:

https://blog.csdn.net/lee_fighting_jingyu/article/details/80021289

https://blog.csdn.net/lee_fighting_jingyu/article/details/80009407

 

*7.6(修改程序清单 5-15)

略,读不懂题目,有大神会的请给我留言,谢谢┭┮﹏┭┮(快哭了)

 

*7.7(统计一位数的个数)

public class S7 {

	public static void main(String[] args) {
		
		int[] numbers=new int[100];
		int[] counts=new int[10];
		
		for(int i=0; i<100; i++) {
			numbers[i]=(int) (Math.random()*10);
			counts[numbers[i]]++;
		}
		
		for(int i=0; i<10; i++) {
			System.out.println(i+" 出现的次数为: "+counts[numbers[i]]+" 次."); 
		}
		
		
	}

}

 

(7.6~7.8节)

7.8(求数组的平均值)

import java.util.Scanner;

public class S8 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入10个doublex型的值:");
		double[] n=new double[10];
		for(int i=0; i<10; i++) {
			n[i]=input.nextDouble();
		}
		
		
		System.out.println("平均数为: "+average(n));
		
		
		
		
	}

	public static int average(int[] array) {
		int count=0;
		int sum=0;
		for(int i=0; i<array.length; i++) {
			count++;
			sum += array[i];
		}
		
		return sum/count;
		
	}
	
	public static double average(double[] array) {
		int count=0;
		double sum=0;
		for(int i=0; i<array.length; i++) {
			count++;
			sum += array[i];
		}
		
		return sum/count;
		
	}
}

 

7.9(找出最小元素)

import java.util.Scanner;

public class S9 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter ten numbers:");
		
		double[] n=new double[10];
		for(int i=0; i<10; i++) {
			n[i]=input.nextDouble();
		}
		
		System.out.println("The minimum number is:"+min(n));

	}
	
	public static double min(double[] array) {
		double min=array[0];
		for(int i=1; i<array.length; i++) {
			if(array[i]<min)
				min=array[i];
		}
		
		return min;
		
	}

}

 

7.10(找出最小元素的下标)

import java.util.Scanner;

public class S10 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter ten numbers:");
		
		double[] n=new double[10];
		for(int i=0; i<10; i++) {
			n[i]=input.nextDouble();
		}
		
		System.out.println("The minimum's index is:"+indexOfSmallestElement(n));
		

	}
	
	public static int indexOfSmallestElement(double[] array) {
		int indexOfSmall=0;
		double min=array[0];
		
		if(array.length>1) {
		for(int i=1; i<array.length; i++) {
			if(array[i]<min) {
				min=array[i];
				indexOfSmall=i;
			  }
				
		    }
		
			return indexOfSmall;
		}
		else {
			return 0;
		}
		
		
	}

}

 

*7.11(统计学方面:计算标准差)

import java.util.Scanner;

public class S11 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter ten numbers:");
		
		double[] n=new double[10];
		for(int i=0; i<10; i++) {
			n[i]=input.nextDouble();
		}
		
		input.close();
		
		System.out.printf("The mean is: %4.2f \n",mean(n));
		System.out.printf("The standard deviation is: %7.5f",deviation(n));
	}
	
	//计算平均值
	public static double mean(double[] x) {
		double mean=0;
		double sum=0;
		for(int i=0; i<x.length; i++) {
			sum += x[i];
		}
		
		mean=sum/x.length;
		
		return mean;
		
	}
	
	//计算标准差
	public static double deviation(double[] x) {
		double deviation=0;
		double sum=0;
		
		for(int i=0; i<x.length; i++) {
			sum += Math.pow( (x[i]-mean(x)), 2 );
		}
		
		deviation=Math.sqrt(sum/(x.length-1));
		
	
		return deviation;
		
	}

}

 

*7.12(倒置数组)

import java.util.Scanner;

public class S12 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入10个整数: ");
		
		int[] numbers=new int[10];
		for(int i=0; i<numbers.length; i++) {
			numbers[i]=input.nextInt();
		}
		
		int[] n1=reverse(numbers);//注意:返回后的数组要再给一个新数组
		
		System.out.print("倒置后的数组为: ");
		for(int i=0; i<10; i++) {
			System.out.print(n1[i]+" ");
		}
		

	}
	
	public static int[] reverse(int[] x) {
		int[] n=new int[x.length];
		
		for(int i=x.length-1,j=0; i>=0 && j<x.length; i--,j++) {
			n[j]=x[i];
		}
		
		return n;
		
	}
}

 

7.9节

*7.13(随机数选择器)

首先题目要多读几遍:(编写一个方法,返回1-54之间的随机数,不包括传递到参数中的numbers

意思是说:产生的这个随机数呀,与numbers数组里的元素不同.

public class S13 {

	public static void main(String[] args) {
		
		//数组1:getRandom(34, 3, 54, 1, 28, 23, 39, 4 );

		//匿名数组 2:getRandom(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
		
		System.out.println("产生与数组 1 里元素各不相同的随机数是:"+getRandom(34, 3, 54, 1, 28, 23, 39, 4 ));  
		System.out.println("产生与数组 2 里元素各不相同的随机数是:"+getRandom(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));   
		
		
	}
	
	public static int getRandom(int... numbers) {
		int random=(int) (1+Math.random()*54);
		for(int i=0; i<numbers.length; i++) {
			//如果产生的随机数和数组中的某元素相等,则重新产生一个随机数
			if(numbers[i] == random) {
				random=(int) (1+Math.random()*54);
			}
				
		}
		return random;
		
		
	}

}

 

7.14(计算gcd)

求最大公约数的方法:(可以参考第五章的编程练习题5.14)

1.首先找到5个数中的最小值 d 。

2.依次检验 d, d-1, d-2,..., 2, 1.是否是这5个数的公约数。

3.当然,第一个满足条件的 d 就是最大公约数。

import java.util.Scanner;

public class S114 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入5个数: ");
		int[] n=new int[5];
		for(int i=0; i<5; i++) {
			n[i]=input.nextInt();
		}
		
		System.out.println("该数组的最大公约数为: "+gcd(n));
		
	}
	
	public static int gcd(int... numbers) {
		
		int d=numbers[0];//d为 5个数中的最小值
		for(int i=1; i<5; i++) {
			if(numbers[i]<d) {
				d=numbers[i];
			}
		}
		
		int gcd=-1;
		
		while(d>=1 && gcd==-1) {
			
			if(numbers[0]%d==0 && numbers[1]%d==0 
			&& numbers[2]%d==0 && numbers[3]%d==0
			&& numbers[4]%d==0) {
				gcd=d;
			}
			
			d--;
			
		}
		return gcd;
		
	}

}

 

(7.10~7.12节)

7.15(消除重复)

我还是不太懂,还是看大神连接吧:(别人的参考答案)

https://blog.csdn.net/qq_36653524/article/details/93508892

https://blog.csdn.net/weixin_38750084/article/details/84196256

 

7.16(执行时间)

public class S16 {

	public static void main(String[] args) {
		
		int key=(int) (Math.random()*100000);
		int[] list=new int[100000];
		
		for(int i=0; i<list.length; i++) {
			list[i]=(int) (Math.random()*100000);
		}
		
		linearSearch(list,key);
		
		binarySearch(list,key);
		
	}
	
	public static void linearSearch(int[] list,int key) {
		long startTime=System.currentTimeMillis();
		for(int i=0; i<list.length; i++) {
			if(key==list[i])
				key=i;
		}
		key=-1;
		
		long endTime=System.currentTimeMillis();
		long executionTime1= endTime - startTime ;
		System.out.println("The linearSearch executionTime is "+executionTime1);
		
		
	}
	
	
	public static void binarySearch(int[] list, int key) {
		java.util.Arrays.sort(list);
		long startTime=System.currentTimeMillis();
		int low=0;
		int height=list.length-1;
		
		while(height >= low) {
			int mid=(low+height)/2;
			if(key<list[mid])
				height=mid-1;
			else if(key==list[mid])
				   key= mid;
			else
				low=mid+1;
		}
		key=-low-1;
		
		long endTime=System.currentTimeMillis();
		long executionTime2= endTime - startTime ;
		System.out.println("The binarySearch executionTime is "+executionTime2);

		
			
	}

}

 

**7.17(对学生排序)

个人感觉应该用面向对象的方法来做比较方便,等我学会了以后再回头来写这题吧,嘻嘻,我先留着

以下是我未完成的代码:(我不知道成绩和名字怎么连接,目前,待我学了对象和类再来做)

import java.util.Scanner;

public class S17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入学生的个数: ");
		int n=input.nextInt();
		
		String[] name=new String[n];
		int[] scores=new int[n];
		
		for(int i=0; i<name.length; i++) {
			System.out.print("请输入"+i+"号学生的姓名和成绩: ");  
			name[i]=input.next();	
			scores[i]=input.nextInt();
			
		}
		
		java.util.Arrays.parallelSort(scores);
		
		for(int i=name.length-1; i>=0; i--) {
			System.out.println(scores[i]+": "+name[i]);
		}
		
		
	}

}

 

**7.18(冒泡排序)

import java.util.Scanner;

public class S18 {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入10个数: ");
		double[] n=new double[10];
		for(int i=0; i<10; i++) {
			n[i]=input.nextDouble();
		}
		
		bubbleSort(n);
		
		input.close();
		
	}

	public static void bubbleSort(double[] n) {
		
		
		for(int i=0; i<n.length-1;i++) {
			for(int j=0; j<n.length-1-i; j++) {
				if(n[j]>=n[j+1]) {
					double temp=n[j];
					n[j]=n[j+1];
					n[j+1]=temp;
					
				}
			}
		}
		System.out.println("冒泡排序:");
		for(int k=0; k<n.length; k++) {
			System.out.printf(" %.1f ", n[k]);
		}
	}
}

 

 

**7.19(是否排好序了?)

import java.util.Scanner;
public class S19 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter list: ");
		
		int number=input.nextInt();
		int[] n=new int[number];
		
		for(int i=0; i<n.length; i++) {
			n[i]=input.nextInt();
		}
		input.close();
	
		if(isSorted(n))
			System.out.println("The list is already sorted");
		else
			System.out.println("The list is not sorted");
		
	}
	
	public static boolean isSorted(int[] list) {
	
		for(int i=1; i<list.length-1 ; i++) {
			if(list[i-1] > list[i]) {
				return false;
			}
		}
		
		return true;
	}
	
}

 

*7.20(修改选择排序)

import java.util.Scanner;

public class S20 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入10个数: ");
		
		double[] n=new double[10];
		for(int i=0; i<10; i++) {
			n[i]=input.nextDouble();
		}
		
		selectionSort(n);

	}
	
	public static void selectionSort(double[] list) {
		for(int i=0;i<list.length-1;i++){
			double currentMax=list[i];
			int currentMaxIndex=i;
			
			for(int j=i+1;j<list.length;j++){
				if(currentMax<list[j]){
					currentMax=list[j];
					currentMaxIndex=j;
				}
			}
			
			if(currentMaxIndex !=i){
				list[currentMaxIndex]=list[i];
				list[i]=currentMax;
			}
		}
		for(int i1=0; i1<list.length; i1++) {
			System.out.print(" "+list[i1]);
		}
	}
}

 

***7.21(游戏:豆机)

不知道,略

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章