C language exercise: Second integer, second integer

Source: Internet
Author: User

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;
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.