TAL good future autumn recruitment programming questions

[Programming question] The smallest k among n numbers

Time limit: 1 second
Space limit: 32768K
Find the smallest k among n numbers
Input description:
Each test input contains n+1 integers separated by spaces, the last integer is the value of k, and n does not exceed 100.
Output description:
Output the smallest k numbers among n integers. output in ascending order.
Input Example 1:
3 9 6 8 -10 7 -11 19 30 12 23 5
Output Example 1:
-11 -10 3 6 7

Parse:

(1) Method 1: Learn from the idea of ​​partition in quicksort. Taking the kth number as the benchmark, place the number less than the benchmark on the left of the benchmark, and place the number greater than the benchmark on the right. In this way, the first k numbers are the smallest k numbers, and then the k numbers are sorted. Therefore, the total time complexity is O(n+klogk).
(2) Method 2: Large top heap. Create a large top heap with a capacity of K, traverse the array, and if there is still room for the large top push, directly add the number to the large top heap. If there is no space, judge whether the maximum value of the top of the heap is greater than the number. If it is greater, replace it and adjust the heap. If it is less than, discard the number directly. Then perform heap sort on the k numbers. The total time complexity is O(nlogk + klogk). Multiset can be used.

C++ code implementation:
Method 1:

#include <iostream>
#include <stdio.h>
#include<algorithm>
using namespace std;

int n=0,k=0;
int num[100];

int partition(int start,int end)
{
    int key = num[start];
    while(start<end){
        while(start<end && num[end]>=key)
            end--;
        num[start] = num[end];
        while(start<end && num[start]<=key)
            start++;
        num[end] = num[start];
    }
    num[start] = key;
    return start;
}

void minKElement()
{
    int left = 0,right=n;
    int index = partition(left,right);
    while(index!=k-1){
        if(index > k-1){
            right = index - 1;
        }
        else
            left = index + 1;
        index = partition(left,right);
    }
    sort(num,num+k);
    for(int i=0; i<k; i++)
        cout<<num[i]<<" ";
}


int main()
{
    while(scanf("%d",&num[n])!=EOF && getchar()!='\n'){
        n++;
    }
    k = num[n--];
    minKElement();
    return 0;
}

Method Two:

#include <iostream>
#include <stdio.h>
#include<algorithm>
using namespace std;

int n=0,k=0;
int num[100];
vector<int> heap;

void heapAdjust(int start,int end)
{
    if(start==end)
        return;
    int rc = heap[start];
    for(int j=2*start; j<=end; j*=2){
        if(j<end && heap[j]<heap[j+1])
            j++;
        if(rc > heap[j])
            break;
        heap[start] = heap[j];
        start = j;
    }
    heap[start] = rc;
}

void minKElement()
{
    int j = 0, i = 0;
    for(; i<=n && j<k; i++,j++){
        heap.push_back(num[i]);
    }
    heapAdjust(0,k-1);  //调整成大顶堆
    for(; i<=n; i++){
       if(num[i]<heap[0]){
            heap[0] = num[i];
            heapAdjust(0,k-1);
        }
    }
    for(i=k-1; i>0; i--){
        j = heap[i];
        heap[i] = heap[0];
        heap[0] = j;
        heapAdjust(0,i-1);
    }
    for(i=0; i<k; i++)
        cout<<heap[i]<<" ";
}

int main()
{
    while(scanf("%d",&num[n])!=EOF && getchar()!='\n'){
        n++;
    }
    k = num[n--];
    minKElement();
    return 0;
}

[Programming question] The number of occurrences of n numbers is greater than or equal to n/2

Time limit: 1 second
Space limit: 32768K
input n integers, and output the number of occurrences greater than or equal to half the length of the array.
Input description:
Each test input contains n integers separated by n spaces, and n does not exceed 100, and there is an integer whose number of occurrences is greater than or equal to n/2.
Output description:
Output the number of occurrences greater than or equal to n/2.
Input Example 1:
3 9 3 2 5 6 7 3 2 3 3 3
Output Example 1:
3

Parse:

(1) Method 1: Use an unordered_map to record a certain number and its number of occurrences, and then traverse the map to find the number whose number exceeds n/2. The time complexity is O(n).
(2) Method 2: Sort. The number in the middle must be the number that appears more than half the time. The time complexity is O(nlogn).
(3) Method 3: Learn from the idea of ​​quick sort, take the first element as the benchmark, put the numbers smaller than the benchmark in front of it, and put the numbers larger than the benchmark behind it. If the position of the fiducial is at n/2, the fiducial is the number that occurs more than half the times. If the position of the fiducial is less than n/2, the number is to the right of the fiducial; if the position of the fiducial is greater than n/2, the number is to the left of the fiducial. The time complexity is O(n).
(4) Method 4: When traversing the array, save two variables: one is a number of the array, and the other is the number of times. When we iterate over the next number, if the number is the same as the number we saved, the count is increased by 1; if it is different, the count is decreased by 1. If the count is 0, we need to save the next number and set the count to 1. The number to look for is the number corresponding to the last time the count was set to 1.

C++ code implementation:
method 1

#include <iostream>
#include <stdio.h>
#include <unordered_map>
using namespace std;

int num[100];
int n = 0;

int halfOfNum()
{
    unordered_map<int,int> dict;
    for(int i=0; i<=n; i++){
        dict[num[i]]++;
    }
    auto it = dict.begin();
    int half = (n-1)/2+1;
    while(it!=dict.end()){
        if(it->second >= half)
            return it->first;
        it++;
    }
    return 0;
}

int main()
{
    while(scanf("%d",&num[n])!=EOF && getchar()!='\n'){
        n++;
    }
    cout<<halfOfNum();
    return 0;
}

Method three:

#include <iostream>
#include <stdio.h>
using namespace std;

int num[100];
int n = 0;

int partition(int start,int end)
{
    int key = num[start];
    while(start<end){
        while(start<end && num[end]>=key)
            end--;
        num[start] = num[end];
        while(start<end && num[start]<=key)
            start++;
        num[end] = num[start];
    }
    num[start] = key;
    return start;
}

int halfOfNum()
{
    int left=0,right=n;
    int middle = (n-1)/2+1;
    int index = partition(left,right);
    while(index!=middle){
        if(index>middle){
            right = index-1;
        }
        else{
            left = index+1;
        }
        index = partition(left,right);
    }
    return num[index];
}

int main()
{
    while(scanf("%d",&num[n])!=EOF && getchar()!='\n'){
        n++;
    }
    cout<<halfOfNum();
    return 0;
}

Method four:

#include <iostream>
#include <stdio.h>
using namespace std;

int num[100];
int n = 0;

int halfOfNum()
{
    int result = num[0];
    int times = 1;
    for(int i=1; i<=n; i++){
        if(times==0){
            result = num[i];
            times=1;
        }
        else if(result==num[i])
            times++;
        else
            times--;
    }
    return result;
}

int main()
{
    while(scanf("%d",&num[n])!=EOF && getchar()!='\n'){
        n++;
    }
    cout<<halfOfNum();
    return 0;
}

[Programming question] Find the longest continuous number string in the string
Time limit: 1 second
Space limit: 32768K
Read a string str, output the longest continuous number string in the string str
Input description: The
test input contains 1 A test case, a string str with a length of no more than 255.
Output description:
Output the longest continuous number string in str in one line.
Input example 1:
abcd12345ed125ss123456789
Output example 1:
123456789

C++ code implementation:

#include <iostream>
#include <stdio.h>
#include<algorithm>
using namespace std;

string longgestNumStr(const string& str)
{
    int len = str.size();
    string result,tmp;
    int longgest = 0;
    for(int i=0; i<len; i++){
        if(str[i]>='0' && str[i]<='9'){
            tmp += str[i];
        }
        else{
            if(tmp.size()>longgest){
                longgest = tmp.size();
                result = tmp;
            }
            tmp.clear();
        }
    }
    if(tmp.size()>longgest){
            longgest = tmp.size();
            result = tmp;
    }
    return result;
}


int main()
{
    string str;
    cin>>str;
    cout<<longgestNumStr(str);
    return 0;
}

[Programming question] Summation

Time limit: 1 second
Space limit: 32768K
Input two integers n and m, randomly select several numbers from the sequence 1, 2, 3....n, and make the sum equal to m, and it is required to list all possible combinations.
Input description:
each test input contains 2 integers, n and m
Output description:
lexicographically arrange the output for each combination, one combination per line
Input example 1:
5 5
Output example 1:
1 4
2 3
5

Parse:

Deep search.

C++ code

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

bool dfs(vector<vector<int>>&result,vector<int>&answer,int rest,int last)
{
    if(rest==0)
        return true;
    int sum = last*(last+1)/2;
    if(rest>sum)
        return false;
    for(int i=last-1; i>=1; i--){
        if(rest>=i){
            answer.push_back(i);
            if(dfs(result,answer,rest-i,i)){
                result.push_back(answer);
            }
            answer.pop_back();
        }
    }
    return false;
}

void sumCombine(int n,int m)
{
    int sum = n*(n+1)/2;
    if(m>sum)
        return;
    vector<vector<int>> result;
    int i = m<=n? m : n;
    for(; i>=1; i--){
        vector<int> answer;
        if(m>=i){
            answer.push_back(i);
            if(dfs(result,answer,m-i,i))
                result.push_back(answer);
        }
    }
    sort(result.begin(),result.end(),[](const vector<int>&a,const vector<int>&b){
        int lenA = a.size(),lenB = b.size();
        int i=lenA-1,j=lenB-1;
        for(; i>=0 &&j>=0; i--,j--){
            if(a[i]!=b[j])
                return a[i]<b[j];
        }
        return false;
    });
    int len = result.size();
    for(int i=0; i<len; i++){
        for(int j=result[i].size()-1; j>=0; j--){
            if(j!=0)
                cout<<result[i][j]<<" ";
            else
                cout<<result[i][j]<<endl;
        }
    }
}

int main()
{
    int n,m;
    cin>>n>>m;
    sumCombine(n,m);
    return 0;
}

[Programming question] Delete common characters

Time limit: 1 second
Space limit: 32768K
Input two strings, delete all characters in the second string from the first string. For example, input "They are students." and "aeiou", the first string after deletion becomes "Thy r stdnts."
Input description:
Each test input contains 2 strings
Output description:
Output after deletion String
Input Example 1:
They are students.
aeiou
Output Example 1:
Thy r stdnts.

Parse:

Use the dictionary unordered_set to record the characters that appear in the second string, traverse each character a of string 1, and add the character to the end of the result string if the character is not found in the dictionary.

C++ code implementation:

#include <iostream>
#include <unordered_set>
using namespace std;

string deleteChar(string&a, string&b)
{
    int lenA = a.size();
    int lenB = b.size();
    int i=0;
    string result;
    unordered_set<char> dict;
    for(;i<lenB; i++)
        dict.insert(b[i]);
    for(i=0; i<lenA; i++){
        if(dict.find(a[i])==dict.end()){
            result += a[i];
        }
    }
    return result;
}


int main()
{
    string a,b;
    getline(cin,a);
    getline(cin,b);
    cout<<deleteChar(a,b);
    return 0;
}

[Programming question] Invert a string

Time limit: 1 second
Space limit: 32768K Invert
the words of a sentence without inverting the punctuation. For example, I like beijing. After the function, it becomes: beijing. like I
Input description:
Each test input contains 1 test case: I like beijing. The length of the input case does not exceed 100
Output description:
Output the inverted string in turn, with Space separation
Input example 1:
I like beijing.
Output example 1:
beijing. like I

C++ code implementation:

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

string reverseString(string &str)
{
    string result;
    int len = str.size();
    string temp;
    vector<string> words;
    for(int i=0; i<len; i++){
        if(str[i]!=' '){
            temp += str[i];
        }
        else{
           words.push_back(temp);
           temp.clear();
        }
    }
    if(str[len-1]!=' ')
        words.push_back(temp);
    for(int i=words.size()-1; i>=0; i--){
        result += words[i];
        if(i!=0)
            result += " ";
    }
    return result;
}


int main()
{
    string str;
    getline(cin,str);
    cout<<reverseString(str);
    return 0;
}

Guess you like

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