C language exercise: Second integer, second integer
Problem description
Write a program and read a group of Integers (no more than 20). When the user inputs 0, the input ends. Then the program will find the second integer from this set of integers and print it out. Note: (1) 0 indicates that the input ends and is not included in this group of integers. (2) In this group of integers, there may be both positive numbers and negative numbers. (3) There are no less than two integers in this group.
Input Format: enter only one line, including several integers, separated by spaces, and the last integer is 0.
Output Format: the integer that outputs the second largest value.
Input and Output sample
Sample Input
5 8-12 7 0
Sample output
7
TIPS: the idea was not very good at the beginning. I wanted to use Bubble sorting to output the second one. I forgot to repeat the maximum value. In the second experiment, I forgot to repeat the minimum value. Finally, I looked at the method for finding the maximum value, and then the two most worthwhile methods came out. I can see that the second limit is always accompanied by the maximum value. You are too weak. I don't know why. The code below will always fail to get the full score, and I don't know which test is wrong. Can enthusiastic netizens try to write and help me? T.T
It is not wise to find the maximum and minimum values of bubbles.
PS: 1, "66,66, 66,66, 66", there is no second largest number, and nothing is output.
2. "99,99, 88,86, 68,66", the maximum number is 88.
3. "0" does not output anything.
# Include <stdio. h>
Int main (void)
{
Int x, y;
Int a [20] = {0 };
Int I =-1;
Int n;
Do
{
I ++;
Scanf ("% d", & a [I]);
} While (a [I]! = 0 & I <19 );
If (I> = 1)
{
X = a [0]; y = a [0];
For (n = 0; n <I; n ++) // assign the maximum value in the array to x
If (x <a [n])
X = a [n];
For (n = 0; n <I; n ++) // assign the second largest value to y
{
If (a [n] = x)
Continue;
Else if (a [n]> y)
Y = a [n];
}
If (x! = Y)
Printf ("% d", y );}
Return 0;
}