C++ error ticket (sorting, input control)

A certain secret-related unit issued a certain kind of bills, which must be recovered at the end of the year.
Each ticket has a unique ID number.
The ID numbers of all bills throughout the year are consecutive, but the starting number of the ID is randomly selected.
Due to the negligence of the staff, an error occurred when entering the ID number, which caused one ID to be out of number and another to be duplicated.
Your task is to find out the ID of the broken number and the ID of the repeated number through programming.
Assume that a broken number cannot occur between the largest and smallest numbers.
Input format The
first line contains the integer N, which means there are N lines of data afterwards. Next N lines, each line contains several (not more than 100) positive integers (not more than 100000) separated by spaces, and each integer represents an ID number.
Output format The
program is required to output one line, containing two integers m, n, separated by spaces.
Among them, m represents the broken ID, and n represents the repeated ID.
Data range
1≤N≤100
Input example:
2
5 6 8 11 9
10 12 9
Output example:
7 9

AC code 1:

#include<stdio.h>
#include<algorithm>

using namespace std;

int line;
int a[10010];
int n,m;
int index=0;

int main()
{
    
    
    scanf("%d",&line);
    //读到结束为止,此用法不受换行符限制
    while(scanf("%d",&a[index++])!=EOF);
    sort(a,a+index);
    for(int i=0;i<index-1;++i)
    {
    
    
        if(a[i+1]-a[i]==2) n=a[i]+1;
        if(a[i+1]==a[i]) m=a[i];
    }
    printf("%d %d",n,m);
    return 0;
}

AC code 2:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>

using namespace std;

int line;
int a[10010];
int n,m;
int index=0;

int main()
{
    
    
    cin>>line;
    string s;
    getline(cin,s);//接收掉第一行的换行符
    while(line--)
    {
    
    
        getline(cin,s);
        stringstream sst(s);
        //必须输入成功再增加index
        //while(sst>>a[index++]);会使输入不成功也增加index
        while(sst>>a[index])++index;
    }
    sort(a,a+index);
    for(int i=1;i<index;++i)
    {
    
    
        if(a[i]-a[i-1]>=2) n=a[i]-1;
        if(a[i]==a[i-1]) m=a[i];
    }
    printf("%d %d",n,m);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44643644/article/details/108860034