Topic Link: http://lx.lanqiao.cn/problem.page?gpid=T28
Problem description
A secret-related unit has issued some kind of bill and has to withdraw it at the end of the year.
Each ticket has a unique ID number. The ID number of all bills is continuous throughout the year, but the starting digit of the ID is randomly selected.
Because the staff negligence, in the entry ID number of a mistake occurred, resulting in a certain ID number, another ID number.
Your task is to programmatically identify the ID of the broken number and the ID of the heavy number.
Suppose the broken number cannot occur in the largest and most small. Input format
Requires that the program first enter an integer N (n<100) to represent the number of subsequent rows of data.
Then read n rows of data.
The length of each row of data, is separated by a number of (no more than 100) positive integers (not greater than 100000), please note that there may be extra space in the line and at the end of the line, your program needs to be able to handle these spaces.
Each integer represents an ID number. Output format
Requires that the program output 1 lines, with two integers m n, separated by a space.
where m represents the id,n of the heavy ID example input 1 2
5 6 8 9
10 12 9 Sample Output 1 7 9 Sample input 2 6
164 178 108 109 180 155 141 159 1 179 118 137 184 (124) 129 196
172 189 127 112 131 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 194 187
113 130 176 154 177 120 117 150 114 183 186 100 163 160 167 147 198 111 119 Sample Output 2 A
train of thought: Because the input has the number of lines, so you can read each line with a string, and then the space for the demarcation point to read each number (here used in C language inside a function atoi ()). In the case of small amount of data can be used for reference to the idea of bucket ordering, open an integer array, used to mark the number of reads, initialization is all 0, so M is the array value of 0 corresponding subscript, n is the array value of 2 corresponding subscript.
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
const int M = 100000+5;
int ar[m];
Char s[10];
int main ()
{
int x,n,m,num,sum=-1;
memset (ar,0, sizeof (AR));
scanf ("%d\n", &x);
for (int i=0; i<x; ++i) {
char c[205];
Gets (c);
int idx=0;
for (int j=0; J<strlen (c); ++j) {
if (c[j] = = ') { //Take the number
Num=atoi (s) between each space;
idx=0;
Memset (s,0,sizeof (s)); Re-empty array
ar[num]++; Mark the digital
//printf ("num=%d\n", num);
Sum=max (sum,num); Used to record the maximum number inside the data, convenient to end the Loop
}
s[idx++] = c[j];
}
Num=atoi (s); The number of idx=0 after the last space
;
Memset (s,0,sizeof (s));
ar[num]++;
Sum=max (Sum,num);
}
int i;
For (i=0 i<=m; ++i) {
if (ar[i]!=0) break;
}
for (i; i<=sum; ++i) { //Here the maximum number of data is used
if (ar[i] = = 0) m = i;
if (ar[i] = = 2) n = i;
}
printf ("%d%d\n", M, n);
return 0;
* About Atoi () function header file: #include <stdlib.h>
The Atoi () function is used to convert a string to an integer (int), and its prototype is:
int atoi (const char * str);
The function description Atoi () function scans the parameter str string, skip the preceding whitespace characters (such as spaces, tab indents, etc., can be detected by the Isspace () function) until a number or positive sign is encountered before the conversion begins, and then when a non-numeric or string is terminated. To end the conversion and return the result.
return value returns the converted integer number, and returns 0 if STR cannot be converted to int or STR is an empty string.