当前位置:网站首页>7-14 error ticket (PTA program design)

7-14 error ticket (PTA program design)

2022-07-06 13:56:00 Programming Lindaiyu

Some secret related unit has issued some kind of bill , And take it back at the end of the year .
Each note has a unique ID Number . Of all bills throughout the year ID The number is continuous , but ID The start number of is randomly selected .
Because of the negligence of the staff , In the input ID There was a mistake on the , Created something ID Break the number , Another one ID Duplicate number .
Your task is to program , Find the broken one ID And the heavy one ID.
Suppose that the interrupt can't happen on the largest and the smallest .

Input format :

Ask the program to enter an integer first N(N< 100) Indicates the number of subsequent data lines .
Then read in N Row data .
The data length of each row is not equal , It's a number of... Separated by spaces ( No more than 100 individual ) Positive integer ( No more than 100000), Please note that there may be extra spaces inside and at the end of the line , Your program needs to be able to handle these spaces .
Each integer represents a ID Number .

Output format :

Request program output 1 That's ok , Two integers m n, Separate... With spaces .
among ,m It's a break ID,n It's a double sign ID

sample input :

Here's a set of inputs . for example :

2
5 6 8 11 9
10 12 9

sample output :

Here is the corresponding output . for example :

7 9

Code (Python): 

list1=[]  # Auxiliary input n Line error ticket 
list2=[]  # Deposit the wrong bill 
n=int(input())  #n Indicates the number of subsequent data lines 
for i in range(n):  # Deposit the wrong note , loop n Times represents the number of data rows 
    list1.clear()  # Before each cycle , take list1 Empty data in 
    list1=list(map(int,input().split()))  # Enter each line of data to list1
    for i in range(len(list1)):  # take list1 Each data in is stored in list2 in 
        list2.append(list1[i])
xiao=min(list2)  # Take the minimum value in the wrong ticket 
da=max(list2)  # Take the maximum value in the wrong ticket 
for i in range(xiao,da+1):  # Look for a hyphen 
    if i not in list2:  # Judge whether each number between the minimum value and the maximum value is list2 in 
        duan=i  # If not , Then the number is a hyphen 
print(duan,end=' ')  # Output hyphen , End with a space 
for i in list2:  # Duplicate number found 
    if list2.count(i)==2:  # use Python Built in functions in count, Statistics list2 The number of occurrences of each element in , If it happens twice , Then the element is a duplicate sign 
        print(i,end='')  # Output duplicate number 
        break  # Because the duplicate number has been found , So exit the loop 

When I first saw this problem, I thought it was very difficult , Because it looks complicated . But I still tried it , It's not difficult to find , Is the most basic mathematical knowledge , There is no hard Algorithm . therefore , When you come across a seemingly difficult problem , And don't give up , Try to do it , To analyze , Maybe it's not very difficult , Or maybe it's really a little difficult , But how do you know if you will not try , What if you make it yourself ? So don't set limits on yourself , Try boldly , To challenge . 

The above program gives more detailed comments , For novice Xiaobai's reference . The idea of program design or code implementation is not optimal , You are welcome to correct your mistakes or give better ideas .

I am a rookie who wants to be Kunpeng , Everyone's encouragement is my driving force , Welcome to like collection comments  

原网站

版权声明
本文为[Programming Lindaiyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060917046920.html