[Daily Blue Bridge] 15. The real question of "Wrong ticket" in the Java group of the provincial competition in the first three years

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Bad Ticket

Title: Bad Ticket

A certain sand kiln unit issued some kind of bills, and all of them were to be recovered at the end of the year

Each bill has a unique ID number. The ID numbers of all bills throughout the year are continuous, but the starting number of the ID is randomly selected.

Due to the negligence of the staff, an error occurred when entering the ID number, which caused one ID to be out of number and another to be duplicated.

Your task is to find out the ID of the broken number and the ID of the repeated number through programming

Assuming that the broken number cannot occur in the largest and smallest numbers

The program is required to input an integer N (N<100) to indicate the number of data rows

Then read in N rows of data

The length of each line of data varies, and is a number (not more than 100) positive integers (not more than 100000) separated by spaces

Each integer represents an ID number

The program is required to input one line, containing two integers m, n, separated by spaces

Among them, m represents the broken ID, n represents the repeated ID

E.g:

User input:

2

5 6 8 11 9

10 12 9

The program output:

7 9

 

Another example:

User input:

6

164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196

172 189 127 107 112 192 103 131 133 169 158

128 102 110 148 139 157 140 195 197

185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190

149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188

113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

The program output:

105 120

 

Resource agreement:

Peak memory consumption (including virtual machines) <64M

CPU consumption <2000ms

Please output strictly according to the requirements, and do not superfluously print redundant content like: "please enter..."

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.

Note: Do not use package statement, do not use jdk1.6 and above features

Note: The name of the main class must be Main, otherwise it will be processed as invalid code

Main points of investigation:

When solving this question, please pay attention: when comparing the same two numbers, the comparison of objects in this program, so the equals method should be used instead of ==, otherwise half of the score will be deducted.

equals compares whether two addresses are equal,

And == is whether the comparison value is equal

Answer source code:

package 一三年省赛真题;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Year2013_Bt7 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		scanner.nextLine();		//读取输入整数后的那个换行符
		ArrayList<Integer> numList = new ArrayList<Integer>();		//存放输入的ID
		for (int i = 0; i < N; i++) {
			String list = scanner.nextLine();		//读取每一行的ID
			String[] split = list.split(" ");		//按照空格将ID进行分割
			for (int j = 0; j < split.length; j++) {
				numList.add(Integer.parseInt(split[j]));	//将ID存放到列表中
			}
		}
		Collections.sort(numList);	//对列表进行排序
		int a = 0,b = 0;
		for (int i = 1; i < numList.size(); i++) {
			//如果当前数与前一个数相差2, 说明中间缺失一个ID
			if (numList.get(i)-numList.get(i-1)==2) {
				a = numList.get(i-1)+1;
			}
			//如果当前数的对象和前一个数的对象相同(即这两个数值重复)
			if (numList.get(i).equals(numList.get(i-1))) {
				b = numList.get(i);
			}
			
		}
		
		System.out.println(a + " " + b);

	}

}

 

 

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the bloggers of the year 2020. Today is the last day. I beg your friends to vote for your support!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/113096908