Problem description has n (n <= 100) integers, which have been arranged in ascending order. Now, to add an integer x, insert the number into the sequence, and make the new sequence still orderly. The input data contains multiple test instances. Each group of data consists of two rows. The first row is N and M, and the second row is a series of ordered n numbers. If n and m are both 0, the end of the input data is indicated. We will not process the data. For each test instance, output is the sequence after the new element is inserted. Sample input3 31 2 40 0 sample output1 2 3 4 time limit exceeded code1:
# Include <stdio. h>
Int main ()
{
Int n, m, I, j, E, A [100], T1, T2;
While (scanf ("% d", & N, & M )! = EOF & (n! = 0 | M! = 0 ))
{
Scanf ("% d", & A [0]);
I = 1;
While (I <n)
{
Scanf ("% d", & A [I]);
If (A [I]> A [I-1])
I ++;
}
If (M> A [n-1])
A [n] = m;
Else
{
For (I = 0; I <n; I ++)
{
If (M <A [I])
{
T1 = A [I];
A [I] = m;
For (j = I + 1; j <= N; j ++)
{
T2 = A [J]; A [J] = T1; T1 = t2;
}
Break;
}
}
}
For (E = 0, I = 0; I <= N; I ++)
{
If (E! = 0)
Printf ("");
Printf ("% d", a [I]);
E ++;
}
Printf ("\ n ");
}
Return 0;
}
Time limit exceeded
Code2:
# Include <stdio. h>
Int main ()
{
Int n, m, I, j, E, A [100], T1, T2, * P =;
While (scanf ("% d", & N, & M )! = EOF & (n! = 0 | M! = 0 ))
{
Scanf ("% d", P );
I = 1;
While (I <n)
{
Scanf ("% d", P + I );
If (* (p + I)> * (p + i-1 ))
I ++;
}
If (M> A [n-1])
A [n] = m;
Else
{
For (I = 0; I <n; I ++)
{
If (M <A [I])
{
T1 = A [I];
A [I] = m;
For (j = I + 1; j <= N; j ++)
{
T2 = A [J]; A [J] = T1; T1 = t2;
}
Break;
}
}
}
For (E = 0, P = A; P <= (a + n); P ++)
{
If (E! = 0)
Printf ("");
Printf ("% d", * P );
E ++;
}
Printf ("\ n ");
}
Return 0;
}