[蓝桥杯 2013 省 AB] 错误票据-java版

[蓝桥杯 2013 省 AB] 错误票据-java版

    • 题目背景
    • 题目描述
    • 输入格式
    • 输出格式
    • 样例 #1
      • 样例输入 #1
      • 样例输出 #1
    • 样例 #2
      • 样例输入 #2
      • 样例输出 #2

题目背景

某涉密单位下发了某种票据,并要在年终全部收回。

题目描述

每张票据有唯一的 ID 号,全年所有票据的 ID 号是连续的,但 ID 的开始数码是随机选定的。因为工作人员疏忽,在录入 ID 号的时候发生了一处错误,造成了某个 ID 断号,另外一个 ID 重号。

你的任务是通过编程,找出断号的 ID 和重号的 ID。

数据保证断号不可能发生在最大和最小号。

输入格式

一个整数 N ( N < 100 ) N(N<100) N(N<100) 表示后面数据行数,接着读入 N N N 行数据,每行数据长度不等,是用空格分开的若干个(不大于 100 100 100 个)正整数(不大于 1 0 5 10^5 105),每个整数代表一个 ID 号。

输出格式

要求程序首先输入要求程序输出 1 1 1 行,含两个整数 m m m n n n,用空格分隔,其中, m m m 表示断号 ID, n n n 表示重号 ID。

样例 #1

样例输入 #1

2
5 6 8 11 9
10 12 9

样例输出 #1

7 9

样例 #2

样例输入 #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
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

样例输出 #2

105 120
//解读: ID 号是连续的,开始数码是随机,断号指的是少了一个数,重号是指重复的数
//思路:对数进行排序,根据连续性,在数学意义上,“连续”的自然数指的是后一个数比前一个数大1,比较便可的断号,再根据set的唯一性,找重号
//步骤:输入n为行数,在n行输入k个数,写一个方法,用list存储
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[10001];
        int l = 0;
        
        int N = scanner.nextInt();
        scanner.nextLine(); // 吃掉换行符
        
        for (int i = 1; i <= N; i++) {
            String s = scanner.nextLine();
//trim()去除首位空格,split("\\s+")表示按空格分割,所以输入时注意输入空格
            String[] parts = s.trim().split("\\s+");
            for (int j = 0; j < parts.length; j++) {
                a[++l] = Integer.parseInt(parts[j]);
            }
        }
        
        Arrays.sort(a, 1, l + 1);
        
        int m = 0, n = 0;
        for (int i = 2; i <= l; i++) {
            if (a[i] == a[i + 1] - 2) {
                m = a[i] + 1;
            } else if (a[i] == a[i + 1]) {
                n = a[i];
            }
            
            // 发现断号和重号后可以提前结束循环,因为题目保证只有一处断号和一处重号
            if (m != 0 && n != 0) break;
        }
        
        System.out.println(m + " " + n);
    }
}
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] ids = new int[10001];
        int length = readIds(scanner, ids);
        
        int missedId = findMissedId(ids, length);
        int duplicateId = findDuplicateId(ids, length);
        
        System.out.println(missedId + " " + duplicateId);
        scanner.close();
    }

    // 读取ID并返回有效ID的数量
    public static int readIds(Scanner scanner, int[] ids) {
        int length = 0;
        int N = scanner.nextInt();
       scanner.nextLine(); // 吃掉换行符,关键,不可以删除,
       //  确保下一次调用 scanner.nextLine() 时能正确读取下一行的字符串内容
        
        for (int i = 1; i <= N; i++) {
            String s = scanner.nextLine();
            String[] parts = s.trim().split("\\s+");
            for (int j = 0; j < parts.length; j++) {
                ids[++length] = Integer.parseInt(parts[j]);
            }
        }
        return length;
    }

    // 查找并返回断号
    public static int findMissedId(int[] ids, int length) {
        Arrays.sort(ids, 1, length + 1);
        for (int i = 2; i <= length; i++) {//i=2,默认第一个不是断号,i<=length防止溢出
            if (ids[i] == ids[i + 1] - 2) {
                return ids[i] + 1;
            }
        }
        return -1; // 没有找到断号
    }

    // 查找并返回重号
    public static int findDuplicateId(int[] ids, int length) {
        for (int i = 1; i < length; i++) {//重号可能是第一个
            if (ids[i] == ids[i + 1]) {
                return ids[i];
            }
        }
        return -1; // 没有找到重号
    }
}

你可能感兴趣的:(蓝桥杯系列,算法,蓝桥杯)