当前位置:网站首页>Niuke.com Huawei question bank (1~10)

Niuke.com Huawei question bank (1~10)

2022-06-10 01:32:00 wrdoct


1. The length of the last word in the string

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]){
    
    char inPut[5000];
    cin.get(inPut, 5000);
    int strLen = 0;
    while(inPut[strLen] != '\0'){
    
        strLen++; // String length  
    }
    
    int num = 0;int i = 0;
    while(i < strLen){
    
        if(inPut[i] != ' '){
    
            i++;
            num++;
        }
        
        if(inPut[i] == ' '){
    
            i++;
            num = 0;
        }
    }
    
    cout << num <<endl;
}

2. Count the number of occurrences of a character

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]){
    
    /*char inPut[1000]; cin.get(inPut, 1000); cin.ignore(); char inStr; cin>>inStr; int strLen = 0; while(inPut[strLen] != '\0'){ strLen++; } int num = 0; for(int j = 0; j < strLen; j++){ if(inStr >= '0' && inStr <= '9'){ if(inPut[j] == inStr) num++; } else{ if(inPut[j] == inStr || (inPut[j]^32) == inStr) num++; } }*/
    string s;
    getline(cin,s);
    char inStr;
    cin>>inStr;
    int num = 0;
    for(int j = 0; j < s.size(); j++){
    
        if(inStr >= '0' && inStr <= '9'){
    
            if(s[j] == inStr) num++;
        }
        else{
    
            if(s[j] == inStr || (s[j]^32) == inStr) num++;
        }
    }
    
    cout << num << endl;
    
    return 0;
}

3. A clear random number

#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main(){
    
    int N = 0;
    cin>>N;
    
    priority_queue<int> pq;
    
    for(int i = 0; i < N; i++){
    
        int num = 0;
        cin>>num;
        pq.push(num);
    }
    
    stack<int> st;
    st.push(pq.top());
    pq.pop();
    while(!pq.empty()){
    
        if(st.top() != pq.top()){
    
            st.push(pq.top());
        }
        pq.pop();
    }
    
    vector<int> res;
    while(!st.empty()){
    
        res.push_back(st.top());
        st.pop();
    }
    
    for(int i = 0; i < res.size(); i++){
    
        cout << res[i] << endl;
    }
    
    return 0;
}

4. String delimitation

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void process(vector<string>& res, string& s){
    
    while(s.size() % 8 != 0){
    
        s += "0";
    }
    for(int i = 0; i < s.size() - 7; i += 8){
    
        string tmp = s.substr(i, 8);
        //cout<<tmp<<endl;
        res.push_back(tmp);
    }
}

int main(){
    
    string str = "";
    vector<string> res;
    
    while(cin>>str){
    
        process(res, str);
    }
    
    for(int i = 0; i < res.size(); i++){
    
        cout<<res[i]<<endl;
    }
    
    return 0;
}

5. Hexadecimal conversion

#include <unordered_map>
#include <algorithm>
#include <math.h>

using namespace std;

unordered_map<char, int> m = {
    
    {
    '0', 0},
    {
    '1', 1},
    {
    '2', 2},
    {
    '3', 3},
    {
    '4', 4},
    {
    '5', 5},
    {
    '6', 6},
    {
    '7', 7},
    {
    '8', 8},
    {
    '9', 9},
    {
    'A', 10},
    {
    'B', 11},
    {
    'C', 12},
    {
    'D', 13},
    {
    'E', 14},
    {
    'F', 15}
};

void process(string s, int& res){
    
    reverse(s.begin(), s.end());
    //cout<<s<<endl;
    for(int i = 0; i < s.size(); i++){
    
        res += m[s[i]] * pow(16, i);
        //cout<<res<<endl;
    }
}

int main(){
    
    string str = "";
    int res = 0;
    
    while(cin>>str){
    
        process(str, res);
    }
    
    cout<<to_string(res)<<endl;
    
    return 0;
}


6. Prime factor

#include <iostream>
#include <vector>

using namespace std;

int main(){
    
    long num = 0; //
    cin >> num;
    vector<int> res;
    
    for(int i = 2; i * i <= num; ++i){
     //2 Is the smallest prime factor   You only need to verify half of it 2 Is it a qualitative factor 
        //cout<<num<<endl;
        while(num % i == 0){
    
            res.push_back(i);
            num /= i;         
        }        
    }
    // If m=1, be while The cycle is just decomposed by prime numbers , If it is greater than 1, Explain that it has not been completely decomposed ,m That's the last prime number 
    // meanwhile , This sentence can also deal with the special case where the input is a prime number 
    if(num >= 2) res.push_back(num); 
    
    for(int i = 0; i < res.size(); i++){
    
        cout << res[i] << " ";
    }
    cout << endl;
    
    return 0;
}

7. Take an approximation

#include <iostream>

using namespace std;

int main() {
    
    double num;
    while(cin >> num)
        {
    
        cout << static_cast<int>(num + 0.5) << endl; //
    }
}

8. Consolidated statement records

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

/*struct cmp{ bool operator()(pair<int, int>& m, pair<int, int>& n) { return m.first < n.first; } };*/

int main(){
    
    int num = 0;
    cin >> num;
    
    map<int, int> m; //map The interior itself is based on key Is stored in order of size 
    //vector<pair<int, int>> vec;
    for(int i = 0; i < num; i++){
    
        int key = 0, value = 0;
        while(cin>>key>>value){
    
            if(!m[key]){
    
                m[key] = value;
            }
            else m[key] += value;// When it doesn't exist , Accumulate when present 
             
            //vec.push_back(make_pair(key, value));
        }
    }
    
    //sort(vec.begin(), vec.end(), cmp());
    
    //map The interior itself is based on key Is stored in order of size 
    for(map<int, int>::iterator/*auto*/ iter = m.begin(); iter != m.end(); iter++){
    
        cout<<iter->first<<" "<<iter->second<<endl;
    }
    
    return 0;
}

9. Extract non duplicate integers

#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>

using namespace std;

int main(){
    
    int num = 0;
    cin >> num;
    
    string str = to_string(num);
    reverse(str.begin(), str.end());
    unordered_set<char> tmp;
    int res = 0;
    
    for(int i = 0; i < str.size(); i++){
    
        if(tmp.count(str[i]) == 0){
    
            res = res * 10 + (str[i] - '0');
        }
        tmp.insert(str[i]);      
    }
    
    cout << res << endl;
    
    return 0;
}

10. Character count

#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

int main(){
    
    string str;
    cin>>str;
    
    unordered_set<char> s;
    for(char c : str){
    
        s.insert(c);
    }
    
    cout << s.size() << endl;
    
    return 0;
}

原网站

版权声明
本文为[wrdoct]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100107293567.html