Question 1 Statistics
In a scientific research survey, n natural numbers were obtained, and each number did not exceed (1.5) × 10^9 ). It is known that there are no more than 10000 different numbers. Now it is necessary to count the occurrence times of these natural numbers, and output the statistical results in the order of natural numbers from small to large.
Enter Description:
The first line is the integer n, which represents the number of natural numbers.
Line 2 ∼ n each line has a natural number.
8
2
4
2
4
5
100
2
100
Output Description:
Output m lines (M is the number of different numbers in n natural numbers) and output in the order of natural numbers from small to large. Each line outputs two integers, which are natural numbers and the number of occurrences of the number, separated by a space.
2 3
4 2
5 1
100 2
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } Arrays.sort(a); int temp = a[0]; int count = 1; int i; for (i = 1; i < n; i++) { if (temp == a[i]) { count++; } if (temp != a[i]) { System.out.println(a[i - 1] + "," + count); count = 1; } temp = a[i]; } System.out.println(a[i - 1] + "," + count); } }
Question 2 wrong bill
A secret related unit has issued certain bills and wants to recover them all at the end of the year.
Each ticket has a unique ID number. The ID numbers of all bills throughout the year are continuous, but the starting number of ID is randomly selected.
Due to the negligence of the staff, an error occurred when entering the ID number, resulting in a broken ID and a duplicate ID. Your task is to find out the ID of the broken number and the ID of the duplicate number through programming. (assuming that hyphenation cannot occur between the largest and smallest)
Enter description
2
5 6 8 11 9
10 12 9
Output description
7 9
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { int quehao=0,duanhao=0; Scanner scanner = new Scanner(System.in); ArrayList<Integer> arrayList = new ArrayList<>(); int n = scanner.nextInt(); scanner.nextLine();//Eat new lines??? for (int i = 0; i < n; i++) { String s1 = scanner.nextLine();//The next() method reads the blank character and ends l; nextLine() reads the end of carriage return, that is "\ r"; String[] s2= s1.split("\\s+");Splits a string into substrings and returns the result as an array of strings. \\s Represents a space,enter,Blank characters such as line feed +Means one or more for (int j = 0; j < s2.length; j++) { arrayList.add(Integer.parseInt(s2[j])); } } Collections.sort(arrayList);//Sort collection for (int i = 1; i < arrayList.size(); i++) { if(arrayList.get(i)-arrayList.get(i-1)==2){ duanhao=arrayList.get(i)-1; } if(arrayList.get(i)-arrayList.get(i-1)==0){ quehao=arrayList.get(i); } } System.out.println( quehao + " " + duanhao); } }
It's wonderful
Question 3 scholarship
A primary school has recently received a sponsorship and plans to give part of it to the top five students with excellent academic achievements. At the end of the term, each student has three grades: Chinese, mathematics and English. First, sort according to the total score from high to low. If the total scores of the two students are the same, then sort according to the Chinese scores from high to low. If the total scores and Chinese scores of the two students are the same, the students with small student number are required to be in the front. In this way, the ranking of each student is uniquely determined.
Task: first calculate the total score according to the scores of the three courses entered, then sort according to the above rules, and finally output the student number and total score of the top five students according to the ranking order. Note that the scholarships of each of the top five students are different, so you must sort them strictly according to the above rules. For example, in a correct answer, if the output data of the first two lines (two numbers per line: student number and total score) is:
7 279
5 279
The meaning of these two lines of data is that the student numbers of the two students with the highest total scores are No. 7 and No. 5 in turn. The total score of the two students is 279 (the total score is equal to the sum of the input scores of Chinese, mathematics and English), but the Chinese score of the student with student number 7 is higher. If your top two output data is:
5 279
7 279
It is treated as an output error and cannot be scored.
Sample input:
6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
Sample output:
6 265
4 264
3 258
2 244
1 237
import java.util.*; public class Main { private static class Grade { Integer num; Integer chinese; Integer grade; public Grade(int num, int chinese, int grade) { this.num = num; this.chinese = chinese; this.grade = grade; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = Integer.parseInt(scanner.nextLine()); Set<Grade> set = new TreeSet<>(new Comparator<Grade>() {//set is auto sort public int compare(Grade grade1, Grade grade2) { int result1 = -grade1.grade.compareTo(grade2.grade); if (result1 != 0) {//Compare results first return result1; } int result2 = -grade1.chinese.compareTo(grade2.chinese); if (result2 != 0) {//Post comparison Chinese achievement return result2; } return grade1.num.compareTo(grade2.num);//Last student number } }); for (int i = 1; i <= num; i++) { String[] array = scanner.nextLine().split(" "); int chinese = Integer.parseInt(array[0]); int grade = chinese + Integer.parseInt(array[1]) + Integer.parseInt(array[2]); set.add(new Grade(i, chinese, grade)); } int counter = 0; for (Grade grade : set) { counter++; System.out.println(grade.num + " " + grade.grade); if (counter == 5) { break; } } scanner.close(); } }
Writing Comparable is too abstract