Beijing University of Posts and Telecommunications retest _2010_ find small k contains the same elements

Title Description

Finding a small number of array K, the same attention as large as the size of the operator
as 213,452 third decimal 3 as
in (parentheses is a comment)
input:
6 (array length n-)
2. 5. 1 3 2 2 ( array)
3 (K is the third decimal)
output:
3

Analysis: The first element and then sorted according to pre judge whether the current comparison of the same

void find_k()
    {
        int n;
        cin >> n;
        vector<int> nums;
        if (n == 0)
        {
            return;
        }
        int t;
        for (int i = 0; i < n; i++)
        {
            cin >> t;
            nums.push_back(t);
        }
        sort(nums.begin(), nums.end());
        int k;
        cin >> k;
        t = 1;
        int i = 1;
        int pre = nums[0];
        while (i < nums.size() && t < k)
        {
            if (pre != nums[i])
            {
                t++;
                pre = nums[i];
            }
            i++;
        }
        if (t == k)
        {
            cout << nums[i - 1] << endl;
        }
        else
        {
            cout << 0 << endl;
        }
    }
Published 63 original articles · won praise 14 · views 10000 +

Guess you like

Origin blog.csdn.net/luncy_yuan/article/details/104328315