java语言程序设计基础篇第七章编程练习题

1

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		int maxScore = 0;
		Scanner input = new Scanner(System.in);
		int numStudent = input.nextInt();
		int[] student = new int[numStudent];
		for(int i = 0; i < numStudent; ++i){
			student[i] = input.nextInt();
			if(student[i] >= maxScore)
				maxScore = student[i];
		}
		
		for(int i = 0; i < numStudent; ++i){
			System.out.print("Student " + i + " score is " + student[i] + " and grade is ");
			if(student[i] >= maxScore - 10)
				System.out.println("A");
			else if(student[i] >= maxScore - 20)
				System.out.println("B");
			else if(student[i] >= maxScore - 30)
				System.out.println("C");
			else
				System.out.println("D");
		}
	}
}

2

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int[] num = new int[10];
		for(int i = 0; i < 10; ++i){
			num[i] = input.nextInt();
		}
		
		for(int i = 9; i >= 0; --i)
			System.out.print(num[i] + " ");
	}
}

3

import java.util.Scanner;  
import java.util.Arrays;
  
public class Main{
    public static void main(String[] args){
    	final int inf = 101;
    	Scanner input = new Scanner(System.in);
    	int[] num = new int[100];
    	int count = 0;
    	while(true){
    		int temp = input.nextInt();
    		if(temp == 0)
    			break;
    		num[count] = temp;
    		++count;
    	}
    	Arrays.sort(num,0,count);
    	int time = 0;
    	for(int i = 0; i < count; ++i){
    		if(num[i] != inf){
    			time = 0;
	    		for(int j = 0; j < count; ++j){
	    			if(num[i] == num[j]){
	    				++time;
	    				if(i != j)
	    					num[j] = inf;
	    			}
	    		}
	    		System.out.println(num[i] + " occurs " + time + (time > 1 ? " times" : " time"));
	    	}
    	}
    }
}

4

import java.util.Scanner;  
import java.util.Arrays;
  
public class Main{
    public static void main(String[] args){
    	Scanner input = new Scanner(System.in);
    	int count = input.nextInt();
    	double[] num = new double[count];
    	double sum = 0;
    	for(int i = 0; i < count; ++i){
    		num[i] = input.nextDouble();
    		sum += num[i];
    	}
    	double average = sum/count;
    	
    	int higher = 0;
    	int lower = 0;
    	int equal = 0;
    	for(int i = 0; i < count; ++i){
    		if(num[i] > average)
    			++higher;
    		else if(num[i] == average)
    			++equal;
    		else
    			++lower;
    	}
    	
    	System.out.println(higher + "    " + equal + "    " + lower);
    }
}
5

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] nums = new int[100];
		System.out.print("Enter ten numbers:");
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			int n = cin.nextInt();
			++nums[n];
		}
		
		System.out.print("The number of distinct number is ");
		int count = 0;
		for(int i = 0; i < 100; ++i)
			if(nums[i] != 0)
				++count;
		System.out.println(count);
		
		for(int i = 0; i < 100; ++i){
			if(nums[i] != 0)
				System.out.print(i + " ");
		}
	}
}


6

。。。。。。。

7

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] nums = new int[10];
		for(int i = 0; i < 100; ++i){
			++nums[(int)(Math.random()*10)];
		}
		
		for(int i = 0; i < 10; ++i){
			System.out.println(i + "    " + nums[i]);
		}
	}
}


8

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		double[] nums = new double[10];
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			nums[i] = cin.nextDouble();
		}
		
		double num = average(nums);
		System.out.println(num);
	}
	
	public static int average(int[] array){
		int sum = 0;
		for(int i = 0; i < array.length; ++i){
			sum += array[i];
		}
		return sum/array.length;
	}
	
	public static double average(double[] array){
		double sum = 0;
		for(int i = 0; i < array.length; ++i){
			sum += array[i];
		}
		return sum/array.length;
	}
}


9

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		double[] nums = new double[10];
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			nums[i] = cin.nextDouble();
		}
		
		double m = min(nums);
		System.out.println(m);
	}

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


10

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		double[] nums = new double[10];
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			nums[i] = cin.nextDouble();
		}
		
		int index = indexOfSmallestElement(nums);
		System.out.println(index);
	}

	
	public static int indexOfSmallestElement(double[] array){
		double m = array[0];
		int index = 0;
		for(int i = 0; i < 10; ++i){
			if(array[i] < m){
				m = array[i];
				index = i;
			}
		}
		return index;
	}
}


11,12

。。。。。。


13

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int n = getRandom(5,6,8,52);
		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;
	}
}


14

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int n = gcd(25,75,100,105,500);
		System.out.println(n);
	}

	public static int gcd(int... numbers){
		int num = numbers[0];
		for(int i = 1; i < numbers.length; ++i){
			num = flounder(num,numbers[i-1]);
		}
		return num;
	}
	
	public static int flounder(int num1, int num2){
		int t = 0;
		while(true){
			t = num1 % num2;
			if(t == 0)
				break;
			num1 = num2;
			num2 = t;
		}
		return num2;
	}
}


15

同第五题

16

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		long startTime = 0;
		long endTime = 0;
		long executionTime = 0;
		int[] nums = new int[100000];
		for(int i = 0; i < 100000; ++i){
			nums[i] = (int)((System.currentTimeMillis()/(i+1))%1000000);
		}
		int key = nums[99999];
		startTime = System.currentTimeMillis();
		int index = linearSearch(nums,key);
		
		endTime = System.currentTimeMillis();
		executionTime = endTime-startTime;
		System.out.println(index + "  " + executionTime);
		
		sort(nums);
		
		startTime = System.currentTimeMillis();
		index = binarySearch(nums,key);
		endTime = System.currentTimeMillis();
		executionTime = endTime-startTime;
		System.out.println(index + "  " + executionTime);
	}
	
	public static void sort(int[] list){
		for(int i = 0; i < list.length-1; ++i){
			for(int j = i+1; j < list.length; ++j){
				if(list[i] > list[j]){
					int temp = list[i];
					list[i] = list[j];
					list[j] = temp;
				}
			}
		}
	}
	
	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;
	}
	
	public static int linearSearch(int[] list, int key){
		for(int i = 0; i < list.length; ++i){
			if(list[i] == key)
				return i;
		}
		return -1;
	}
}
17

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		String[] names = new String[n];
		double[] grades = new double[n];
		
		for(int i = 0; i < n; ++i){
			names[i] = cin.next();
			grades[i] = cin.nextDouble();
		}
		
		for(int i = 0; i < n-1; ++i){
			for(int j = i+1; j < n; ++j){
				if(grades[i] < grades[j]){
					double temp = grades[i];
					grades[i] = grades[j];
					grades[j] = temp;
					
					String tmp = names[i];
					names[i] = names[j];
					names[j] = tmp;
				}
			}
		}
		
		for(int i = 0; i < n; ++i){
			System.out.println(names[i] + "    " + grades[i]);
		}
	}	

}

18

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		double[] nums = new double[n];
		
		for(int i = 0; i < n; ++i){
			nums[i] = cin.nextDouble();
		}
		
		for(int i = 0; i < n-1; ++i){
			for(int j = 0; j < n-1; ++j){
				if(nums[j] < nums[j+1]){
					double temp = nums[j];
					nums[j] = nums[j+1];
					nums[j+1] = temp;
				}
			}
		}
		
		for(int i = 0; i < n; ++i){
			System.out.println(nums[i]);
		}
	}	

}

19

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.println("Enter list:");
		int n = cin.nextInt();
		int[] nums = new int[n];
		for(int i = 0; i < n; ++i)
			nums[i] = cin.nextInt();
		
		for(int i = 1; i < n; ++i){
			if(nums[i] < nums[i-1]){
				System.out.println("The list is not sorted");
				return ;
			}
		}
		System.out.println("The list is already sorted");
	}	
}

20

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.println("Enter list:");
		int n = cin.nextInt();
		double[] nums = new double[n];
		for(int i = 0; i < n; ++i)
			nums[i] = cin.nextDouble();

		for(int i = n-1; i > 0; --i){
			for(int j = i-1; j >= 0; --j){
				if(nums[i] < nums[j]){
					double temp = nums[i];
					nums[i] = nums[j];
					nums[j] = temp;
				}
			}
		}
		
		for(int i = 0; i < n; ++i){
			System.out.println(nums[i]);
		}
	}	
}

21

import java.util.Scanner;

public class Main{
	//仅适合槽的个数为偶数个,还是二维数组比较直观些,一维数组感觉不太直观。。。。
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter the number of balls to drop:");
		int ball = cin.nextInt();
		System.out.print("Enter the number of slots in the bean machine:");
		int slot = cin.nextInt();
		int[][] slots = new int[slot][slot];
		char[] dir = new char[2];
		dir[0] = 'R';
		dir[1] = 'L';
		int limit = slot - 1;
		int flag = 0;
		int record = 0;
		for(int b = 0; b < ball; ++b){
			record = 0;
			for(int i = 0; i < limit; ++i){
				if((flag = (int)(System.currentTimeMillis()/(i+1))%2) == 0){
					record += 1;
					if(record > i+1){
						record -= 2;
						flag = 1;
					}
					System.out.print(dir[flag]);
				}
				else{
					flag = 1;
					record -= 1;
					if(record < -i-1){
						record += 2;
						flag = 0;
					}
					System.out.print(dir[flag]);
				}
			}
			
			if(record > 0){
				int h = limit / 2;
				int n = 0;
				while(true){
					if(slots[n][h+(record+1)/2] == 0){
						slots[n][h+(record+1)/2] = 1;
						break;
					}
					else
						++n;
				}
			}
			else{
				int h = limit / 2 + 1;
				int n = 0;
				while(true){
					
					if(slots[n][h+(record-1)/2] == 0){
						slots[n][h+(record-1)/2] = 1;
						break;
					}
					else
						++n;
				}
			}
			System.out.println();
		}
		
		for(int i = slot-1; i >= 0; --i){
			for(int j = 0; j < slot; ++j){
				if(slots[i][j] == 0)
					System.out.print(" ");
				else
					System.out.print(0);
			}
			System.out.println();
		}
		
	}
}

22

import java.util.Scanner;
//二维数组比较直观
public class Main{
	
	public static void main(String[] args){
		int[][] map = new int[8][8];
		int j = (int)(Math.random()*8);
		int pre = 0;
		for(int i = 0; i < 8; ++i){
			if(i == 0)
				map[i][j] = 1;
			if(i != 0){
				while(true){
					j = (int)(Math.random()*8);
					if(j != pre && (i-1-pre) != i-j){
						map[i][j] = 1;
						break;
					}
				}
			}
			pre = j;
		}
		
		for(int i = 0; i < 8; ++i){
			for(int v = 0; v < 8; ++v){
				System.out.print(map[i][v]);
			}
			System.out.println();
		}
		
	}
}

23

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		boolean[] box = new boolean[101];
		int flag = 0;
		for(int i = 1; i <= 100; ++i){
			if(i == 1){
				for(int j = 1; j <= 100; ++j)
					box[j] = !box[j];
			}
			else if(i == 2){
				for(int j = i; j <= 100; j += 2){
					box[j] = !box[j];
				}
			}
			else{
				for(int j = i; j <= 100; j += i){
					box[j] = !box[j];
				}
			}
		}
		
		for(int i = 1; i <= 100; ++i){
			if(box[i])
				System.out.println(i);
		}
	}
}

24

其实我没有看懂题目

25

。。。。。

26

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] nums1 = new int[10];
		int[] nums2 = new int[10];
		Scanner cin = new Scanner(System.in);
		
		for(int i = 0; i < 10; ++i){
			nums1[i] = cin.nextInt();
		}
		
		for(int i = 0; i < 10; ++i){
			nums2[i] = cin.nextInt();
		}
		
		System.out.println(equals(nums1,nums2));
	}
	
	public static boolean equals(int[] list1, int[] list2){
		for(int i = 0; i < list1.length; ++i){
			if(list1[i] != list2[i])
				return false;
		}
		return true;
	}
}

27

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] nums1 = new int[10];
		int[] nums2 = new int[10];
		Scanner cin = new Scanner(System.in);
		
		for(int i = 0; i < 10; ++i){
			nums1[i] = cin.nextInt();
		}
		
		for(int i = 0; i < 10; ++i){
			nums2[i] = cin.nextInt();
		}
		
		System.out.println(equals(nums1,nums2));
	}
	
	public static boolean equals(int[] list1, int[] list2){
		int[] book = new int[list1.length];
		int count = 0;
		for(int i = 0; i < list2.length; ++i){
			for(int j = 0; j < list1.length; ++j){
				if(list2[i] == list1[j] && book[j] == 0){
					++count;
					book[j] = 1;
					break;
				}
			}
		}
		if(count == list1.length)
			return true;
		else
			return false;
	}
}

28

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] nums = new int[10];
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			nums[i] = cin.nextInt();
		}
		
		for(int i = 0; i < 10; ++i){
			for(int j = i+1; j < 10; ++j){
				System.out.print(nums[i] + "," + nums[j] + "  ");
			}
			System.out.println();
		}
	}
	
}

29

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] nums = new int[13];
		Scanner cin = new Scanner(System.in);
		for(int i = 0; i < 13; ++i){
			nums[i] = i+1;
		}
		int count = 0;
		for(int i = 0; i < 13; ++i){
			for(int j = 0; j < 13; ++j){
				for(int n = 0; n < 13; ++n){
					for(int v = 0; v < 13; ++v){
						if(nums[i] + nums[j] + nums[n] + nums[v] == 24)
							++count;
					}
				}
			}
		}
		System.out.println(count);
	}
}

30

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter the number of values:");
		int num = cin.nextInt();
		int[] nums = new int[num];
		System.out.print("Enter the values:");
		for(int i = 0; i < num; ++i){
			nums[i] = cin.nextInt();
		}
		System.out.println(isConsecutiveFour(nums));
	}
	
	public static boolean isConsecutiveFour(int[] values){
		int count = 0;
		for(int i = 0; i < values.length; ++i){
			count = 0;
			for(int j = i; j < values.length; ++j){
				if(values[i] == values[j]){
					++count;
					if(count == 4)
						return true;
				}
			}
		}
		return false;
	}
}

31

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] arr1 = new int[6];
		int[] arr2 = new int[5];
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter list1:");
		for(int i = 0; i < arr1.length; ++i)
			arr1[i] = cin.nextInt();
		System.out.print("Enter list2:");
		for(int i = 0; i < arr2.length; ++i)
			arr2[i] = cin.nextInt();
		
		int[] result = merge(arr1, arr2);
		
		for(int i = 0; i < result.length; ++i){
			if(result[i] != 0){
				for(int j = 0; j < result[i]; ++j)
					System.out.print(i + " ");
			}
		}
	}

	public static int[] merge(int[] list1, int[] list2){
		int max = 0;
		for(int i = 0; i < list1.length; ++i){
			if(list1[i] > max)
				max = list1[i];
		}
		
		for(int i = 0; i < list2.length; ++i){
			if(list2[i] > max)
				max = list2[i];
		}
		
		int[] array = new int[max+1];
		
		for(int i = 0; i < list1.length; ++i)
			++array[list1[i]];
		
		for(int i = 0; i < list2.length; ++i)
			++array[list2[i]];
		
		return array;
	}
}

32

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int[] nums = new int[10];
		for(int i = 0; i < 10; ++i){
			nums[i] = cin.nextInt();
		}
		int num = partition(nums);
		System.out.println(num);
		for(int i = 0; i < nums.length; ++i)
			System.out.print(nums[i] + " ");
	}

	public static int partition(int[] list){
		int temp = 0;
		int j = 0;
		for(int i = 1; i < list.length; ++i){
			if(list[i] <= list[j]){
				temp = list[i];
				list[i] = list[j];
				list[j] = temp;
				
				temp = list[j+1];
				list[j+1] = list[i];
				list[i] = temp;
				
				++j;
			}
		}
		return j;
	}
}

33

。。。。。。。

34

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		String str = cin.nextLine();
		str = sort(str);
		System.out.println(str);
	}

	public static String sort(String s){
		char[] chs = s.toCharArray();
		java.util.Arrays.sort(chs);
		String result = String.valueOf(chs);
		return result;
	}
}

35

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		String[] words = {"write","word","that","exit","property","length","long"};
		int n = words.length;
		Scanner cin = new Scanner(System.in);
		while(true){
			int num = (int)(Math.random()*n);
			
			char[] word = new char[words[num].length()];
			for(int i = 0; i < words[num].length(); ++i){
				word[i] = words[num].charAt(i);
			}
			
			int len = word.length;
			int count = 0;
			int flag = 0;
			int mark = 0;
			char[] guess = new char[len];
			char cTemp;
			String STemp = null;
			java.util.Arrays.fill(guess, '*');
			while(true){
				mark = 0;
				System.out.print("(Guess)Enter a letter in word ");
				System.out.print(String.valueOf(guess) + " >");
				STemp = cin.nextLine(); 
				cTemp = STemp.charAt(0);
				for(int i = 0; i < len; ++i){
					if(cTemp == word[i] && guess[i] != '*'){
						System.out.println(cTemp + " is already in the word");
						break;
					}
					if(cTemp == word[i] && guess[i] == '*'){
						mark = 1;
						++count;
						guess[i] = cTemp;
						if(count == len){
							flag = 1;
							break;
						}
					}
				}
				if(mark == 0)
					System.out.println(cTemp + " is not in the word");
				if(flag == 1)
					break;
			}
			System.out.println("The word is " + String.valueOf(word) + ". You missed 1 time" );
			System.out.print("Do you want to guess another word? Enter y or n >");
			String judge = cin.nextLine();
			if(judge.charAt(0) == 'n' || judge.charAt(0) == 'n')
				break;
		}
	}
}



你可能感兴趣的:(Java学习)