[Preguntas de la prueba provincial de 2013] Notas incorrectas


Directorio de artículos


Billete incorrecto

   某涉密单位下发了某种票据,并要在年终全部收回。
    • 每张票据有唯一的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()); // 输出集合的大小
    }

}

Se informará un error aquí: lo Exception in thread "main" java.lang.NumberFormatException: For input string: ""verificó y dijo que síCuando se convierte al tipo de datos int, el valor nulo no se puede realizar.
Inserte la descripción de la imagen aquí
Aquí, mi entendimiento personal es que el juicio del contenido de la consola es incorrecto. La 2única representación aquí es 数据的行数Nque después de la asignación, el recorrido solo tiene el efecto de controlar el número de filas a recorrer. Entonces en este momentoEl contenido transversal de la consola son en realidad las siguientes dos líneas, Primero debe sc.nextLine()moverse hacia abajo una línea antes de atravesar , en otras palabras, comerse el carácter de nueva línea después de 2 \n.

Inserte la descripción de la imagen aquí
El siguiente paso es atravesar los elementos en los puntos de interrupción de la colección, ID repetidos:

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);
    }

}

Inserte la descripción de la imagen aquí
prueba:

Inserte la descripción de la imagen aquí

Volver arriba


Supongo que te gusta

Origin blog.csdn.net/qq_45797116/article/details/113850215
Recomendado
Clasificación