错误票据
分析:
1. 本题在原理上不难,在输入是要尤为注意
2. 因为从第二行开始输入的数据没有确定的个数,可以将每行用nextLine()读取。
3. 然后用split()进行分割,再转化为整数。
4. 用set去重的方法找出重复值
5. 用数组索引的对应boolean值 找出缺失值
package java_2013_A;
import java.util.*;
public class Main007_错误票据 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
int M = 0, N = 0, max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
Set<Integer> set = new HashSet<Integer>();
boolean [] b = new boolean[100000];
for (int i = 0; i < n; i++) {
String s = in.nextLine();
String [] m = s.split(" ");
for (int j = 0; j < m.length; j++) {
int t = Integer.parseInt(m[j]);
b[t]=true;
int l1 = set.size();
set.add(t);
if(set.size()==l1) N=t;
if(min>t) min = t;
if(max<t) max = t;
}
}
for(int i=min;i<=max;i++){
if(b[i]==false) M=i;
}
System.out.println(M+" "+N);
}
}