[Java] 藍橋杯PREV-5 歷屆試題 錯誤票據
問題描述
某涉密單位下發了某種票據,並要在年終全部收回。
每張票據有唯一的ID號。全年所有票據的ID號是連續的,但ID的開始數碼是隨機選定的。
因為工作人員疏忽,在錄入ID號的時候發生了一處錯誤,造成了某個ID斷號,另外一個ID重號。
你的任務是通過程式設計,找出斷號的ID和重號的ID。
輸入格式
要求程式首先輸入一個整數N(N<100)表示後面資料行數。 接著讀入N行資料。 每行資料長度不等,是用空格分開的若干個(不大於100個)正整數(不大於100000),請注意行內和行末可能有多餘的空格,你的程式需要能處理這些空格。 每個整數代表一個ID號。
輸出格式
要求程式輸出1行,含兩個整數m n,用空格分隔。
樣例輸入1
2
5 6 8 11 9
樣例輸出1
7 9
樣例輸入2
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
樣例輸出2
105 120
package prev5; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); int min = 100000; int max = 0; int[] corrent = new int[100000]; for (int i = 0; i < n; i++) { String[] id = in.nextLine().split(" "); for (int j = 0; j < id.length; j++) { if (!id[j].equals("")) { int num = Integer.parseInt(id[j]); corrent[num]++; if (num < min) { min = num; } if (max < num) { max = num; } } } } int dupilcate = 0, emp = 0; for (int i = min; i <= max; i++) { if (corrent[i] == 0) { emp = i; } else if (corrent[i] == 2) { dupilcate = i; } } System.out.println(emp + " " + dupilcate); in.close(); } }ofollow,noindex" target="_blank">❤❤點選這裡 -> 訂閱PAT、藍橋杯、GPLT天梯賽、LeetCode題解離線版❤❤
