The Blue Bridge algorithm improves the second largest integer

Original title link: http://lx.lanqiao.cn/problem.page?gpid=T211

Welcome to my Blue Bridge Cup OJ Problem Solution~ https://blog.csdn.net/richenyunqi/article/details/80192062

 Algorithm to raise second largest integer  
Time limit: 1.0s Memory limit: 512.0MB
Problem Description
  Write a program that reads in a set of integers (no more than 20), and when the user enters 0, the input is over. Then the program will find the second largest integer from the set of integers and print it out. Explanation: (1) 0 indicates the end of input, which itself is not counted in this set of integers. (2) In this set of integers, there are both positive and negative numbers. (3) The number of this group of integers is not less than 2.
  Input format: The input is only one line, including several integers, separated by spaces, and the last integer is 0.
  Output format: output the second largest integer.
  Input and output example
sample input
5 8 -12 7 0
Sample output
7

c++ code:

#include<bits/stdc++.h>
using namespace std;  
bool cmp(int a,int b){
    return a>b;
}
int main() {  
    int a,i=0,num[22];  
    while(scanf("%d",&a),a){  
        num[i]=a;  
        i++;  
    }  
    sort(num,num+i,cmp);  
    printf("%d\n",num[1]);  
    return 0;  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325738235&siteId=291194637