[2013 Provincial Test Questions] Wrong Notes


Article Directory


Wrong ticket

   某涉密单位下发了某种票据,并要在年终全部收回。
    • 每张票据有唯一的ID号。
    • 全年所有票据的ID是连续的,但ID的开始数码是随机选定的。
    因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。
    你的任务:
      • 通过编程,找出断号的ID和重号的ID
    
    假设断号不可能发生在最大和最小号。
    要求程序:
      • 首先输入一个整数(N<100)表示后面数据行数。
      • 接着读入N行数据
      • 每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于10000)
      • 每个整数代表一个ID号。
     • 程序输出1行,含两个整数mn,用空格分隔。其中,m表示断号ID,n表示重号ID
  
    例如
      用户输入:
       2
       5 6 8 11 9
       10 12 9
      则程序输出:
       7 9

    资源约定
    峰值内存消耗(含虚拟机<64M);CPU消耗<2000ms
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入”的多余内容
public class Test05_错误票据 {
    
    

    public static void main(String[] args) {
    
    
        // 创建集合存储数据
        ArrayList<Integer> list = new ArrayList<>();
        // 控制台录入
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(); // N 表示数据的行数
        // 遍历N行数据
        for (int i = 0; i < N; i++) {
    
    
            String line = sc.nextLine(); 
            String[] splits = line.split(" "); // 对每一行数据进行拆分
            for (int j = 0; j < splits.length; j++) {
    
    
                list.add(Integer.parseInt(splits[j])); // 将数据存入集合
            }
        }
        System.out.println(list.size()); // 输出集合的大小
    }

}

An error will be reported here:, Exception in thread "main" java.lang.NumberFormatException: For input string: ""checked it and said yesWhen converted to int data type, the null value cannot be realized.
Insert picture description here
Here, my personal understanding is that the judgment of the content of the console is wrong. The 2only representation here is 数据的行数Nthat after the assignment, the traversal only has the effect of controlling the number of rows to traverse. So at this timeThe console traversal content is actually the next two lines, You should first sc.nextLine()move down one line before traversing , in other words, eat the newline character after 2 \n.

Insert picture description here
The next step is to traverse the elements in the collection-breakpoints, repeated IDs:

package 蓝桥杯;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;

public class Test05_错误票据 {
    
    

    public static void main(String[] args) {
    
    
        // 创建集合存储数据
        ArrayList<Integer> list = new ArrayList<>();
        // 控制台录入
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.nextLine(); // 更新控制台内容 --- 吃掉整数后面的换行符
        // 遍历
        for (int i = 0; i < N; i++) {
    
    
            String line = sc.nextLine();
            String[] splits = line.split(" ");
            for (int j = 0; j < splits.length; j++) {
    
    
                list.add(Integer.parseInt(splits[j]));
            }
        }

        // 遍历集合判断断点、重复ID
        // 1.首先进行排序
        Collections.sort(list); // 对于集合的排序用Collections.sort()
        // 2.遍历
        int m = 0,n = 0; // 用于接收断点、重复值
        for (int i = 1; i < list.size(); i++) {
    
    
            if (list.get(i) - list.get(i-1) == 2){
    
    
                m = list.get(i)-1;
            }
            if (list.get(i)==list.get(i-1)){
    
    
                n = list.get(i);
            }
        }
        System.out.println(m+" "+n);
    }

}

Insert picture description here
test:

Insert picture description here

Back to top


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/113850215