天天看點

藍橋杯第四屆真題 :錯誤票據

點選檢視:藍橋杯曆年真題 題解目錄

錯誤票據

藍橋杯第四屆真題 :錯誤票據
藍橋杯第四屆真題 :錯誤票據
分析:
1. 本題在原理上不難,在輸入是要尤為注意
2. 因為從第二行開始輸入的資料沒有确定的個數,可以将每行用nextLine()讀取。
3. 然後用split()進行分割,再轉化為整數。
4. 用set去重的方法找出重複值
5. 用數組索引的對應boolean值 找出缺失值
           
package java_2013_A;

/**
 * @author Ren
 */
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]); // 将分割的字元串轉換為int
                b[t]=true;                      // 根據boolean記錄出現的數字
                int l1 = set.size();            // 記錄添加資料之前的set集合大小
                set.add(t);
                if(set.size()==l1)  N=t;        // 如果添加資料後 set集合的大小沒有發生變化,說明遇到重複元素
                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);
    }
}

           
藍橋杯第四屆真題 :錯誤票據
藍橋杯第四屆真題 :錯誤票據
藍橋杯第四屆真題 :錯誤票據

繼續閱讀