L1-079 Ladder of Goodness (20 points)

L1-079 Kindness in Ladders (20 points)
Ladders are a game of kindness. The kind-hearted proposition team hopes to control the difficulty of the questions within a certain range, so that every participating student has a problem that can be solved, and the best students have to work very hard to get a high score.

So the proposition group first divided the programming ability into 10
6
​ levels (too crazy, this is fake), and then investigated the programming ability of each participating student. Now please write a program to find out the minimum and maximum ability values ​​of all participating students, and give it to the proposition group as a reference for the problem.

Input format:
Enter a positive integer N (≤2×10
​4
​ ) in the first line, which is the total number of participating students. The next line gives N positive integers not exceeding 10
6 ​​, which are the ability values ​​of the participating students.

Output format:
The first line outputs the minimum ability value of all participating students and the number of students with this ability value. The second line outputs the maximum ability value of all participating students and the number of students with this ability value. Numbers in the same row are separated by 1 space, and there must be no extra spaces at the beginning and end of the line.

Input sample:

10
86 75 233 888 666 75 886 888 75 666

Sample output:

75 3
888 2

Use an array to do it:

#include<iostream>
using namespace std;

int main(){
    
    
    int N;
    cin>>N;
    int Array[N];
    for(int i=0;i<N;i++){
    
    
        cin>>Array[i];
    }
    int max_cnt = 0;
    int min_cnt = 0;
    int max = Array[0];
    int min = Array[0];
    
    for(int j=1;j<N;j++){
    
    
        if(Array[j]>max){
    
    
            max = Array[j];
        }
        if(Array[j]<min){
    
    
            min = Array[j];
        }
    }
    for(int x=0;x<N;x++){
    
    
        if(Array[x]==max){
    
    
            max_cnt++;
        }
        if(Array[x]==min){
    
    
            min_cnt++;
        }

    }
    cout<<min<<" "<<min_cnt<<endl;
    cout<<max<<" "<<max_cnt<<endl;
    return 0;
}

Or directly use the method in <algorithm>:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    
    
    int N,num;
    vector<int>v;
    cin>>N;
    for(int i=0;i<N;i++){
    
    
        cin>>num;
        v.push_back(num);
    }
    int max_cnt = 0;
    int min_cnt = 0;
    auto biggest = std::max_element(std::begin(v), std::end(v));
    //std::vector<double>::iterator biggest = std::max_element(std::begin(v), std::end(v));
    auto smallest = std::min_element(std::begin(v), std::end(v));
    for(auto it = v.begin();it!=v.end();it++){
    
    
        if(*it == *biggest){
    
    
            max_cnt++;
        }
        if(*it == *smallest){
    
    
            min_cnt++;
        }
    }
    cout<<*smallest<<" "<<min_cnt<<endl;
    cout<<*biggest<<" "<<max_cnt<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_46368082/article/details/118711698