2021-09-28 Find an element located in k-th location

Input the number in your array:
14 9
3 4 8 12 15 15 23 26 26 30 31 33 33 36
1 3 4 5 5 13 24 24 44
The 1th number is 1
The 2th number is 3
The 3th number is 4
The 4th number is 4
The 5th number is 5
The 6th number is 5
The 7th number is 5
The 8th number is 8
The 9th number is 12
The 10th number is 13
The 11th number is 15
The 12th number is 15
The 13th number is 23
The 14th number is 24
The 15th number is 24
The 16th number is 26
The 17th number is 26
The 18th number is 30
The 19th number is 31
The 20th number is 33
The 21th number is 33
The 22th number is 36
The 23th number is 44

The codes:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
int *create_array(int *a, int n);
void dis_array(int *a, int n);
int find_kth_value(int a[], int length_a, int b[], int length_b, int k);
int main()
{
    srand((unsigned)time(NULL));
    cout << "Input the number in your array:"<< endl;
    int n1, n2;
    cin >> n1 >> n2;
    int *a, *b;
    a = create_array(a, n1);
    sort(a, a+n1);
    dis_array(a, n1);
    b = create_array(b, n2);
    cout << endl;
    sort(b, b+n2);
    dis_array(b, n2);
    cout << endl;
    for(int k=1; k<=n1+n2; ++k){
        cout << "The " << k << "th number is "<< find_kth_value(a, n1, b, n2, k) << endl;
    }
    return 0;
}
int *create_array(int *a, int n)
{
    a = new int[n];
    for(int i=0; i<n; ++i){
        a[i]= rand()%45;
        //cout << a[i] <<endl;
    }
    return a;
}
void dis_array(int *a, int n)
{
    for(int i=0; i<n; ++i){
        cout << a[i] << " ";
    }
}
int find_kth_value(int a[], int length_a, int b[], int length_b, int k)
{
    if(k <0){
        return -1;
    }
    if(k == 1){
        return a[0]>b[0]? b[0]:a[0];
    }
    if(length_a == 0){
        return b[k-1];
    }
    if(length_a > length_b){
        return find_kth_value(b, length_b, a, length_a, k);
    }else{
        int numa = length_a > (k/2)? (k/2):length_a;
        int numb = k - numa;
        if(a[numa-1] == b[numb-1]){
            return a[numa];
        }else if(a[numa-1] < b[numb-1]){
            return find_kth_value(&a[numa], length_a-numa, b, length_b, k-numa);
        }else{
            return find_kth_value(a, length_a, &b[numb], length_b-numb, k-numb);
        }

    }
}

The second try

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
int *create_array(int *a, int n);
void dis_array(int *a, int n);
int find_kth_value(int a[], int length_a, int b[], int length_b, int k);
int main()
{
    srand((unsigned)time(NULL));
    cout << "Input the number in your array:"<< endl;
    int n1, n2;
    cin >> n1 >> n2;
    int *a, *b;
    a = create_array(a, n1);
    sort(a, a+n1);
    dis_array(a, n1);
    cout << endl;
    b = create_array(b, n2);
    sort(b, b+n2);
    dis_array(b, n2);
    cout << endl;
    for(int k=1; k<=n1+n2; ++k){
        cout << "The "<< k << "th value is " << find_kth_value(a, n1, b, n2, k) << endl;
    }
    return 0;
}
int *create_array(int *a, int n)
{
    a = new int[n];
    for(int i=0; i<n; ++i){
        a[i]= rand()%15;
        //cout << a[i] <<endl;
    }
    return a;
}
void dis_array(int *a, int n)
{
    for(int i=0; i<n; ++i){
        cout << a[i] << " ";
    }
}
int find_kth_value(int a[], int length_a, int b[], int length_b, int k)
{
    if(k < 0){
        return -1;
    }
    if(length_a > length_b){
        return find_kth_value(b, length_b, a, length_a, k);
    }
    if(length_a == 0){        // the exit of the recursion algorithm
        return b[k-1];
    }
    if(k ==1){                // the exit of the recursion algorithm
        return (a[0]<b[0]? a[0]:b[0]);
    }
    int num_a = length_a < k/2 ? length_a: k/2;
    int num_b = k - num_a;
    if(a[num_a-1] == b[num_b-1]){            // the devil is in the details 
        return a[num_a -1];
    }else if(a[num_a-1] > b[num_b-1]){        
        return find_kth_value(a, length_a, &b[num_b], length_b-num_b, k-num_b);
    }else{
        return find_kth_value(&a[num_a], length_a-num_a, b, length_b, k-num_a);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38396940/article/details/120538539
今日推荐