读题

ID开始数随机,是连续的,工作人员疏忽,出现了断号和重号。。通过程序恢复正确数字

核心思想

方法1:boolean值

重号:每个数只要用过就设置为true,定义一个布尔数组(用于判断每个数是否出现过),如果出现过了则用一个temp标示一下,
断号:写一个start(start通过极大值筛选出来最小的就是初始值),如果对应的boolean不为空则+1,如果为空start就是断号

方法2:排序

重号:前后两个一样
断号:前后两个差值 >= 2

读入

1.拿到每一行输入的字符串
2.根据split挨个获得目标字符串数组
3.将获得的字符串数组里的元素挨个转换成int添加到数组中

  1. /*赋值*/
  2. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  3. int row = Integer.parseInt(bufferedReader.readLine());
  4. /*数组大小*/
  5. int count = 0;
  6. for (int i = 0; i < row; i++) {
  7. strings[i] = bufferedReader.readLine();
  8. String[] strings1 = strings[i].split(" ");
  9. for (String s: strings1) {
  10. nums[count++] = Integer.parseInt(s);
  11. }
  12. }

代码

package lanqiaobei.meiju;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class bill {
    static String[] strings = new String[1000];
    static int[] nums = new int[1000];
    static boolean[] aBoolean = new boolean[30];
    public static void main(String[] args) throws IOException {
        /*赋值*/
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int row = Integer.parseInt(bufferedReader.readLine());
        /*数组大小*/
        int count = 0;
        for (int i = 0; i < row; i++) {
            strings[i] = bufferedReader.readLine();
            String[] strings1 = strings[i].split("  ");
            for (String s: strings1) {
                nums[count++] = Integer.parseInt(s);
            }
        }
        /*计算*/
        /*重数*/
        int repeat = 0;
        int min = 100010;
        for (int i = 0;i < count;i++) {
            if (aBoolean[nums[i]]){
                repeat = nums[i];
            }
            if (nums[i]<min){
                min = nums[i];
            }
            aBoolean[nums[i]] = true;
        }
        System.out.println(repeat);
        /*缺数*/
        int start = min;
        int deficit = 0;
        while (true){
            if (!aBoolean[start]){
                deficit = start;
                break;
            }
            start++;
        }
        System.out.println(deficit);
    }
}